xref: /original-bsd/usr.bin/make/compat.c (revision 50cee248)
1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * Copyright (c) 1988, 1989 by Adam de Boor
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)compat.c	5.9 (Berkeley) 05/07/93";
15 #endif /* not lint */
16 
17 /*-
18  * compat.c --
19  *	The routines in this file implement the full-compatibility
20  *	mode of PMake. Most of the special functionality of PMake
21  *	is available in this mode. Things not supported:
22  *	    - different shells.
23  *	    - friendly variable substitution.
24  *
25  * Interface:
26  *	Compat_Run	    Initialize things for this module and recreate
27  *	    	  	    thems as need creatin'
28  */
29 
30 #include    <stdio.h>
31 #include    <sys/types.h>
32 #include    <sys/stat.h>
33 #include    <sys/wait.h>
34 #include    <sys/errno.h>
35 #include    <signal.h>
36 #include    <ctype.h>
37 #include    "make.h"
38 extern int errno;
39 
40 /*
41  * The following array is used to make a fast determination of which
42  * characters are interpreted specially by the shell.  If a command
43  * contains any of these characters, it is executed by the shell, not
44  * directly by us.
45  */
46 
47 static char 	    meta[256];
48 
49 static GNode	    *curTarg = NILGNODE;
50 static GNode	    *ENDNode;
51 static int  	    CompatRunCommand();
52 
53 /*-
54  *-----------------------------------------------------------------------
55  * CompatInterrupt --
56  *	Interrupt the creation of the current target and remove it if
57  *	it ain't precious.
58  *
59  * Results:
60  *	None.
61  *
62  * Side Effects:
63  *	The target is removed and the process exits. If .INTERRUPT exists,
64  *	its commands are run first WITH INTERRUPTS IGNORED..
65  *
66  *-----------------------------------------------------------------------
67  */
68 static void
69 CompatInterrupt (signo)
70     int	    signo;
71 {
72     GNode   *gn;
73     struct stat sb;
74 
75     if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
76 	char 	  *file = Var_Value (TARGET, curTarg);
77 
78 	if (!stat(file, &sb) && S_ISREG(sb.st_mode) &&
79 	    unlink (file) == SUCCESS) {
80 	    printf ("*** %s removed\n", file);
81 	}
82 
83 	/*
84 	 * Run .INTERRUPT only if hit with interrupt signal
85 	 */
86 	if (signo == SIGINT) {
87 	    gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
88 	    if (gn != NILGNODE) {
89 		Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
90 	    }
91 	}
92     }
93     exit (0);
94 }
95 
96 /*-
97  *-----------------------------------------------------------------------
98  * CompatRunCommand --
99  *	Execute the next command for a target. If the command returns an
100  *	error, the node's made field is set to ERROR and creation stops.
101  *
102  * Results:
103  *	0 if the command succeeded, 1 if an error occurred.
104  *
105  * Side Effects:
106  *	The node's 'made' field may be set to ERROR.
107  *
108  *-----------------------------------------------------------------------
109  */
110 static int
111 CompatRunCommand (cmd, gn)
112     char    	  *cmd;	    	/* Command to execute */
113     GNode   	  *gn;    	/* Node from which the command came */
114 {
115     char    	  *cmdStart;	/* Start of expanded command */
116     register char *cp;
117     Boolean 	  silent,   	/* Don't print command */
118 		  errCheck; 	/* Check errors */
119     union wait 	  reason;   	/* Reason for child's death */
120     int	    	  status;   	/* Description of child's death */
121     int	    	  cpid;	    	/* Child actually found */
122     int	    	  numWritten;	/* Number of bytes written for error message */
123     ReturnStatus  stat;	    	/* Status of fork */
124     LstNode 	  cmdNode;  	/* Node where current command is located */
125     char    	  **av;	    	/* Argument vector for thing to exec */
126     int	    	  argc;	    	/* Number of arguments in av or 0 if not
127 				 * dynamically allocated */
128     Boolean 	  local;    	/* TRUE if command should be executed
129 				 * locally */
130 
131     silent = gn->type & OP_SILENT;
132     errCheck = !(gn->type & OP_IGNORE);
133 
134     cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
135     cmdStart = Var_Subst (cmd, gn, FALSE);
136 
137     /*
138      * brk_string will return an argv with a NULL in av[1], thus causing
139      * execvp to choke and die horribly. Besides, how can we execute a null
140      * command? In any case, we warn the user that the command expanded to
141      * nothing (is this the right thing to do?).
142      */
143 
144     if (*cmdStart == '\0') {
145 	Error("%s expands to empty string", cmd);
146 	return(0);
147     } else {
148 	cmd = cmdStart;
149     }
150     Lst_Replace (cmdNode, (ClientData)cmdStart);
151 
152     if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
153 	(void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
154 	return(0);
155     } else if (strcmp(cmdStart, "...") == 0) {
156 	gn->type |= OP_SAVE_CMDS;
157 	return(0);
158     }
159 
160     while ((*cmd == '@') || (*cmd == '-')) {
161 	if (*cmd == '@') {
162 	    silent = TRUE;
163 	} else {
164 	    errCheck = FALSE;
165 	}
166 	cmd++;
167     }
168 
169     /*
170      * Search for meta characters in the command. If there are no meta
171      * characters, there's no need to execute a shell to execute the
172      * command.
173      */
174     for (cp = cmd; !meta[*cp]; cp++) {
175 	continue;
176     }
177 
178     /*
179      * Print the command before echoing if we're not supposed to be quiet for
180      * this one. We also print the command if -n given.
181      */
182     if (!silent || noExecute) {
183 	printf ("%s\n", cmd);
184 	fflush(stdout);
185     }
186 
187     /*
188      * If we're not supposed to execute any commands, this is as far as
189      * we go...
190      */
191     if (noExecute) {
192 	return (0);
193     }
194 
195     if (*cp != '\0') {
196 	/*
197 	 * If *cp isn't the null character, we hit a "meta" character and
198 	 * need to pass the command off to the shell. We give the shell the
199 	 * -e flag as well as -c if it's supposed to exit when it hits an
200 	 * error.
201 	 */
202 	static char	*shargv[4] = { "/bin/sh" };
203 
204 	shargv[1] = (errCheck ? "-ec" : "-c");
205 	shargv[2] = cmd;
206 	shargv[3] = (char *)NULL;
207 	av = shargv;
208 	argc = 0;
209     } else {
210 	/*
211 	 * No meta-characters, so no need to exec a shell. Break the command
212 	 * into words to form an argument vector we can execute.
213 	 * brk_string sticks our name in av[0], so we have to
214 	 * skip over it...
215 	 */
216 	av = brk_string(cmd, &argc);
217 	av += 1;
218     }
219 
220     local = TRUE;
221 
222     /*
223      * Fork and execute the single command. If the fork fails, we abort.
224      */
225     cpid = vfork();
226     if (cpid < 0) {
227 	Fatal("Could not fork");
228     }
229     if (cpid == 0) {
230 	if (local) {
231 	    execvp(av[0], av);
232 	    numWritten = write (2, av[0], strlen (av[0]));
233 	    numWritten = write (2, ": not found\n", sizeof(": not found"));
234 	} else {
235 	    (void)execv(av[0], av);
236 	}
237 	exit(1);
238     }
239 
240     /*
241      * The child is off and running. Now all we can do is wait...
242      */
243     while (1) {
244 	int 	  id;
245 
246 	if (!local) {
247 	    id = 0;
248 	}
249 
250 	while ((stat = wait((int *)&reason)) != cpid) {
251 	    if (stat == -1 && errno != EINTR) {
252 		break;
253 	    }
254 	}
255 
256 	if (stat > -1) {
257 	    if (WIFSTOPPED(reason)) {
258 		status = reason.w_stopval;		/* stopped */
259 	    } else if (WIFEXITED(reason)) {
260 		status = reason.w_retcode;		/* exited */
261 		if (status != 0) {
262 		    printf ("*** Error code %d", status);
263 		}
264 	    } else {
265 		status = reason.w_termsig;		/* signaled */
266 		printf ("*** Signal %d", status);
267 	    }
268 
269 
270 	    if (!WIFEXITED(reason) || (status != 0)) {
271 		if (errCheck) {
272 		    gn->made = ERROR;
273 		    if (keepgoing) {
274 			/*
275 			 * Abort the current target, but let others
276 			 * continue.
277 			 */
278 			printf (" (continuing)\n");
279 		    }
280 		} else {
281 		    /*
282 		     * Continue executing commands for this target.
283 		     * If we return 0, this will happen...
284 		     */
285 		    printf (" (ignored)\n");
286 		    status = 0;
287 		}
288 	    }
289 	    break;
290 	} else {
291 	    Fatal ("error in wait: %d", stat);
292 	    /*NOTREACHED*/
293 	}
294     }
295 
296     return (status);
297 }
298 
299 /*-
300  *-----------------------------------------------------------------------
301  * CompatMake --
302  *	Make a target.
303  *
304  * Results:
305  *	0
306  *
307  * Side Effects:
308  *	If an error is detected and not being ignored, the process exits.
309  *
310  *-----------------------------------------------------------------------
311  */
312 static int
313 CompatMake (gn, pgn)
314     GNode   	  *gn;	    /* The node to make */
315     GNode   	  *pgn;	    /* Parent to abort if necessary */
316 {
317     if (gn->type & OP_USE) {
318 	Make_HandleUse(gn, pgn);
319     } else if (gn->made == UNMADE) {
320 	/*
321 	 * First mark ourselves to be made, then apply whatever transformations
322 	 * the suffix module thinks are necessary. Once that's done, we can
323 	 * descend and make all our children. If any of them has an error
324 	 * but the -k flag was given, our 'make' field will be set FALSE again.
325 	 * This is our signal to not attempt to do anything but abort our
326 	 * parent as well.
327 	 */
328 	gn->make = TRUE;
329 	gn->made = BEINGMADE;
330 	Suff_FindDeps (gn);
331 	Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
332 	if (!gn->make) {
333 	    gn->made = ABORTED;
334 	    pgn->make = FALSE;
335 	    return (0);
336 	}
337 
338 	if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
339 	    Var_Set (IMPSRC, Var_Value(TARGET, gn), pgn);
340 	}
341 
342 	/*
343 	 * All the children were made ok. Now cmtime contains the modification
344 	 * time of the newest child, we need to find out if we exist and when
345 	 * we were modified last. The criteria for datedness are defined by the
346 	 * Make_OODate function.
347 	 */
348 	if (DEBUG(MAKE)) {
349 	    printf("Examining %s...", gn->name);
350 	}
351 	if (! Make_OODate(gn)) {
352 	    gn->made = UPTODATE;
353 	    if (DEBUG(MAKE)) {
354 		printf("up-to-date.\n");
355 	    }
356 	    return (0);
357 	} else if (DEBUG(MAKE)) {
358 	    printf("out-of-date.\n");
359 	}
360 
361 	/*
362 	 * If the user is just seeing if something is out-of-date, exit now
363 	 * to tell him/her "yes".
364 	 */
365 	if (queryFlag) {
366 	    exit (-1);
367 	}
368 
369 	/*
370 	 * We need to be re-made. We also have to make sure we've got a $?
371 	 * variable. To be nice, we also define the $> variable using
372 	 * Make_DoAllVar().
373 	 */
374 	Make_DoAllVar(gn);
375 
376 	/*
377 	 * Alter our type to tell if errors should be ignored or things
378 	 * should not be printed so CompatRunCommand knows what to do.
379 	 */
380 	if (Targ_Ignore (gn)) {
381 	    gn->type |= OP_IGNORE;
382 	}
383 	if (Targ_Silent (gn)) {
384 	    gn->type |= OP_SILENT;
385 	}
386 
387 	if (Job_CheckCommands (gn, Fatal)) {
388 	    /*
389 	     * Our commands are ok, but we still have to worry about the -t
390 	     * flag...
391 	     */
392 	    if (!touchFlag) {
393 		curTarg = gn;
394 		Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
395 		curTarg = NILGNODE;
396 	    } else {
397 		Job_Touch (gn, gn->type & OP_SILENT);
398 	    }
399 	} else {
400 	    gn->made = ERROR;
401 	}
402 
403 	if (gn->made != ERROR) {
404 	    /*
405 	     * If the node was made successfully, mark it so, update
406 	     * its modification time and timestamp all its parents. Note
407 	     * that for .ZEROTIME targets, the timestamping isn't done.
408 	     * This is to keep its state from affecting that of its parent.
409 	     */
410 	    gn->made = MADE;
411 #ifndef RECHECK
412 	    /*
413 	     * We can't re-stat the thing, but we can at least take care of
414 	     * rules where a target depends on a source that actually creates
415 	     * the target, but only if it has changed, e.g.
416 	     *
417 	     * parse.h : parse.o
418 	     *
419 	     * parse.o : parse.y
420 	     *  	yacc -d parse.y
421 	     *  	cc -c y.tab.c
422 	     *  	mv y.tab.o parse.o
423 	     *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
424 	     *
425 	     * In this case, if the definitions produced by yacc haven't
426 	     * changed from before, parse.h won't have been updated and
427 	     * gn->mtime will reflect the current modification time for
428 	     * parse.h. This is something of a kludge, I admit, but it's a
429 	     * useful one..
430 	     *
431 	     * XXX: People like to use a rule like
432 	     *
433 	     * FRC:
434 	     *
435 	     * To force things that depend on FRC to be made, so we have to
436 	     * check for gn->children being empty as well...
437 	     */
438 	    if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
439 		gn->mtime = now;
440 	    }
441 #else
442 	    /*
443 	     * This is what Make does and it's actually a good thing, as it
444 	     * allows rules like
445 	     *
446 	     *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
447 	     *
448 	     * to function as intended. Unfortunately, thanks to the stateless
449 	     * nature of NFS (and the speed of this program), there are times
450 	     * when the modification time of a file created on a remote
451 	     * machine will not be modified before the stat() implied by
452 	     * the Dir_MTime occurs, thus leading us to believe that the file
453 	     * is unchanged, wreaking havoc with files that depend on this one.
454 	     *
455 	     * I have decided it is better to make too much than to make too
456 	     * little, so this stuff is commented out unless you're sure it's
457 	     * ok.
458 	     * -- ardeb 1/12/88
459 	     */
460 	    if (noExecute || Dir_MTime(gn) == 0) {
461 		gn->mtime = now;
462 	    }
463 	    if (DEBUG(MAKE)) {
464 		printf("update time: %s\n", Targ_FmtTime(gn->mtime));
465 	    }
466 #endif
467 	    if (!(gn->type & OP_EXEC)) {
468 		pgn->childMade = TRUE;
469 		Make_TimeStamp(pgn, gn);
470 	    }
471 	} else if (keepgoing) {
472 	    pgn->make = FALSE;
473 	} else {
474 	    printf ("\n\nStop.\n");
475 	    exit (1);
476 	}
477     } else if (gn->made == ERROR) {
478 	/*
479 	 * Already had an error when making this beastie. Tell the parent
480 	 * to abort.
481 	 */
482 	pgn->make = FALSE;
483     } else {
484 	if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
485 	    Var_Set (IMPSRC, Var_Value(TARGET, gn), pgn);
486 	}
487 	switch(gn->made) {
488 	    case BEINGMADE:
489 		Error("Graph cycles through %s\n", gn->name);
490 		gn->made = ERROR;
491 		pgn->make = FALSE;
492 		break;
493 	    case MADE:
494 		if ((gn->type & OP_EXEC) == 0) {
495 		    pgn->childMade = TRUE;
496 		    Make_TimeStamp(pgn, gn);
497 		}
498 		break;
499 	    case UPTODATE:
500 		if ((gn->type & OP_EXEC) == 0) {
501 		    Make_TimeStamp(pgn, gn);
502 		}
503 		break;
504 	}
505     }
506 
507     return (0);
508 }
509 
510 /*-
511  *-----------------------------------------------------------------------
512  * Compat_Run --
513  *	Initialize this mode and start making.
514  *
515  * Results:
516  *	None.
517  *
518  * Side Effects:
519  *	Guess what?
520  *
521  *-----------------------------------------------------------------------
522  */
523 void
524 Compat_Run(targs)
525     Lst	    	  targs;    /* List of target nodes to re-create */
526 {
527     char    	  *cp;	    /* Pointer to string of shell meta-characters */
528     GNode   	  *gn;	    /* Current root target */
529     int	    	  errors;   /* Number of targets not remade due to errors */
530 
531     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
532 	signal(SIGINT, CompatInterrupt);
533     }
534     if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
535 	signal(SIGTERM, CompatInterrupt);
536     }
537     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
538 	signal(SIGHUP, CompatInterrupt);
539     }
540     if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
541 	signal(SIGQUIT, CompatInterrupt);
542     }
543 
544     for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
545 	meta[*cp] = 1;
546     }
547     /*
548      * The null character serves as a sentinel in the string.
549      */
550     meta[0] = 1;
551 
552     ENDNode = Targ_FindNode(".END", TARG_CREATE);
553     /*
554      * If the user has defined a .BEGIN target, execute the commands attached
555      * to it.
556      */
557     if (!queryFlag) {
558 	gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
559 	if (gn != NILGNODE) {
560 	    Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
561 	}
562     }
563 
564     /*
565      * For each entry in the list of targets to create, call CompatMake on
566      * it to create the thing. CompatMake will leave the 'made' field of gn
567      * in one of several states:
568      *	    UPTODATE	    gn was already up-to-date
569      *	    MADE  	    gn was recreated successfully
570      *	    ERROR 	    An error occurred while gn was being created
571      *	    ABORTED	    gn was not remade because one of its inferiors
572      *	    	  	    could not be made due to errors.
573      */
574     errors = 0;
575     while (!Lst_IsEmpty (targs)) {
576 	gn = (GNode *) Lst_DeQueue (targs);
577 	CompatMake (gn, gn);
578 
579 	if (gn->made == UPTODATE) {
580 	    printf ("`%s' is up to date.\n", gn->name);
581 	} else if (gn->made == ABORTED) {
582 	    printf ("`%s' not remade because of errors.\n", gn->name);
583 	    errors += 1;
584 	}
585     }
586 
587     /*
588      * If the user has defined a .END target, run its commands.
589      */
590     if (errors == 0) {
591 	Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
592     }
593 }
594