VirtualBox

source: kBuild/trunk/src/kmk/compat.c@ 30

Last change on this file since 30 was 27, checked in by bird, 22 years ago

OS2 / VAC308

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 KB
Line 
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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40#if 0
41static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
42#else
43static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $";
45#endif
46#endif /* not lint */
47
48/*-
49 * compat.c --
50 * The routines in this file implement the full-compatibility
51 * mode of PMake. Most of the special functionality of PMake
52 * is available in this mode. Things not supported:
53 * - different shells.
54 * - friendly variable substitution.
55 *
56 * Interface:
57 * Compat_Run Initialize things for this module and recreate
58 * thems as need creatin'
59 */
60
61#include <stdio.h>
62#include <sys/types.h>
63#include <sys/stat.h>
64#ifdef __IBMC__
65#else
66#include <sys/wait.h>
67#endif
68#include <ctype.h>
69#include <errno.h>
70#include <signal.h>
71#include "make.h"
72#include "hash.h"
73#include "dir.h"
74#include "job.h"
75
76/*
77 * The following array is used to make a fast determination of which
78 * characters are interpreted specially by the shell. If a command
79 * contains any of these characters, it is executed by the shell, not
80 * directly by us.
81 */
82
83static char meta[256];
84
85static GNode *curTarg = NILGNODE;
86static GNode *ENDNode;
87static void CompatInterrupt __P((int));
88static int CompatRunCommand __P((ClientData, ClientData));
89static int CompatMake __P((ClientData, ClientData));
90
91static char *sh_builtin[] = {
92 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
93 "unalias", "umask", "unset", "wait", ":", 0};
94
95/*-
96 *-----------------------------------------------------------------------
97 * CompatInterrupt --
98 * Interrupt the creation of the current target and remove it if
99 * it ain't precious.
100 *
101 * Results:
102 * None.
103 *
104 * Side Effects:
105 * The target is removed and the process exits. If .INTERRUPT exists,
106 * its commands are run first WITH INTERRUPTS IGNORED..
107 *
108 *-----------------------------------------------------------------------
109 */
110static void
111CompatInterrupt (signo)
112 int signo;
113{
114 GNode *gn;
115
116 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
117 char *p1;
118 char *file = Var_Value (TARGET, curTarg, &p1);
119
120 if (!noExecute && eunlink(file) != -1) {
121 printf ("*** %s removed\n", file);
122 }
123 efree(p1);
124
125 /*
126 * Run .INTERRUPT only if hit with interrupt signal
127 */
128 if (signo == SIGINT) {
129 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
130 if (gn != NILGNODE) {
131 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
132 }
133 }
134
135 }
136 if (signo == SIGQUIT)
137 exit(signo);
138 (void) signal(signo, SIG_DFL);
139 (void) kill(getpid(), signo);
140}
141
142
143/*-
144 *-----------------------------------------------------------------------
145 * shellneed --
146 *
147 * Results:
148 * Returns 1 if a specified line must be executed by the shell,
149 * 0 if it can be run via execve, and -1 if the command is a no-op.
150 *
151 * Side Effects:
152 * None.
153 *
154 *-----------------------------------------------------------------------
155 */
156static int
157shellneed (cmd)
158 char *cmd;
159{
160 char **av, **p;
161 int ac;
162
163 av = brk_string(cmd, &ac, TRUE);
164 for(p = sh_builtin; *p != 0; p++)
165 if (strcmp(av[1], *p) == 0)
166 return (1);
167 return (0);
168}
169
170
171/*-
172 *-----------------------------------------------------------------------
173 * CompatRunCommand --
174 * Execute the next command for a target. If the command returns an
175 * error, the node's made field is set to ERROR and creation stops.
176 *
177 * Results:
178 * 0 if the command succeeded, 1 if an error occurred.
179 *
180 * Side Effects:
181 * The node's 'made' field may be set to ERROR.
182 *
183 *-----------------------------------------------------------------------
184 */
185static int
186CompatRunCommand (cmdp, gnp)
187 ClientData cmdp; /* Command to execute */
188 ClientData gnp; /* Node from which the command came */
189{
190 char *cmdStart; /* Start of expanded command */
191 register char *cp;
192 Boolean silent, /* Don't print command */
193 errCheck; /* Check errors */
194 int reason; /* Reason for child's death */
195 int status; /* Description of child's death */
196 int cpid; /* Child actually found */
197 ReturnStatus stat; /* Status of fork */
198 LstNode cmdNode; /* Node where current command is located */
199 char **av; /* Argument vector for thing to exec */
200 int argc; /* Number of arguments in av or 0 if not
201 * dynamically allocated */
202 Boolean local; /* TRUE if command should be executed
203 * locally */
204 int internal; /* Various values.. */
205 char *cmd = (char *) cmdp;
206 GNode *gn = (GNode *) gnp;
207
208 /*
209 * Avoid clobbered variable warnings by forcing the compiler
210 * to ``unregister'' variables
211 */
212#if __GNUC__
213 (void) &av;
214 (void) &errCheck;
215#endif
216 silent = gn->type & OP_SILENT;
217 errCheck = !(gn->type & OP_IGNORE);
218
219 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
220 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
221
222 /*
223 * brk_string will return an argv with a NULL in av[0], thus causing
224 * execvp to choke and die horribly. Besides, how can we execute a null
225 * command? In any case, we warn the user that the command expanded to
226 * nothing (is this the right thing to do?).
227 */
228
229 if (*cmdStart == '\0') {
230 free(cmdStart);
231 Error("%s expands to empty string", cmd);
232 return(0);
233 } else {
234 cmd = cmdStart;
235 }
236 Lst_Replace (cmdNode, (ClientData)cmdStart);
237
238 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
239 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
240 return(0);
241 } else if (strcmp(cmdStart, "...") == 0) {
242 gn->type |= OP_SAVE_CMDS;
243 return(0);
244 }
245
246 while ((*cmd == '@') || (*cmd == '-')) {
247 if (*cmd == '@') {
248 silent = DEBUG(LOUD) ? FALSE : TRUE;
249 } else {
250 errCheck = FALSE;
251 }
252 cmd++;
253 }
254
255 while (isspace((unsigned char)*cmd))
256 cmd++;
257
258 /*
259 * Search for meta characters in the command. If there are no meta
260 * characters, there's no need to execute a shell to execute the
261 * command.
262 */
263 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
264 continue;
265 }
266
267 /*
268 * Print the command before echoing if we're not supposed to be quiet for
269 * this one. We also print the command if -n given.
270 */
271 if (!silent || noExecute) {
272 printf ("%s\n", cmd);
273 fflush(stdout);
274 }
275
276 /*
277 * If we're not supposed to execute any commands, this is as far as
278 * we go...
279 */
280 if (noExecute) {
281 return (0);
282 }
283
284 if (*cp != '\0') {
285 /*
286 * If *cp isn't the null character, we hit a "meta" character and
287 * need to pass the command off to the shell. We give the shell the
288 * -e flag as well as -c if it's supposed to exit when it hits an
289 * error.
290 */
291 static char *shargv[4] = { "/bin/sh" };
292
293 shargv[1] = (errCheck ? "-ec" : "-c");
294 shargv[2] = cmd;
295 shargv[3] = (char *)NULL;
296 av = shargv;
297 argc = 0;
298 } else if ((internal = shellneed(cmd))) {
299 /*
300 * This command must be passed by the shell for other reasons..
301 * or.. possibly not at all.
302 */
303 static char *shargv[4] = { "/bin/sh" };
304
305 if (internal == -1) {
306 /* Command does not need to be executed */
307 return (0);
308 }
309
310 shargv[1] = (errCheck ? "-ec" : "-c");
311 shargv[2] = cmd;
312 shargv[3] = (char *)NULL;
313 av = shargv;
314 argc = 0;
315 } else {
316 /*
317 * No meta-characters, so no need to exec a shell. Break the command
318 * into words to form an argument vector we can execute.
319 * brk_string sticks our name in av[0], so we have to
320 * skip over it...
321 */
322 av = brk_string(cmd, &argc, TRUE);
323 av += 1;
324 }
325
326 local = TRUE;
327
328 /*
329 * Fork and execute the single command. If the fork fails, we abort.
330 */
331 cpid = vfork();
332 if (cpid < 0) {
333 Fatal("Could not fork");
334 }
335 if (cpid == 0) {
336 if (local) {
337 execvp(av[0], av);
338 (void) write (2, av[0], strlen (av[0]));
339 (void) write (2, ":", 1);
340 (void) write (2, strerror(errno), strlen(strerror(errno)));
341 (void) write (2, "\n", 1);
342 } else {
343 (void)execv(av[0], av);
344 }
345 exit(1);
346 }
347
348 /*
349 * we need to print out the command associated with this Gnode in
350 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
351 * in main(), Fatal() and DieHorribly(), therefore do not free it
352 * when debugging.
353 */
354 if (!DEBUG(GRAPH2)) {
355 free(cmdStart);
356 Lst_Replace (cmdNode, cmdp);
357 }
358
359 /*
360 * The child is off and running. Now all we can do is wait...
361 */
362 while (1) {
363
364 while ((stat = wait(&reason)) != cpid) {
365 if (stat == -1 && errno != EINTR) {
366 break;
367 }
368 }
369
370 if (stat > -1) {
371 if (WIFSTOPPED(reason)) {
372 status = WSTOPSIG(reason); /* stopped */
373 } else if (WIFEXITED(reason)) {
374 status = WEXITSTATUS(reason); /* exited */
375 if (status != 0) {
376 printf ("*** Error code %d", status);
377 }
378 } else {
379 status = WTERMSIG(reason); /* signaled */
380 printf ("*** Signal %d", status);
381 }
382
383
384 if (!WIFEXITED(reason) || (status != 0)) {
385 if (errCheck) {
386 gn->made = ERROR;
387 if (keepgoing) {
388 /*
389 * Abort the current target, but let others
390 * continue.
391 */
392 printf (" (continuing)\n");
393 }
394 } else {
395 /*
396 * Continue executing commands for this target.
397 * If we return 0, this will happen...
398 */
399 printf (" (ignored)\n");
400 status = 0;
401 }
402 }
403 break;
404 } else {
405 Fatal ("error in wait: %d", stat);
406 /*NOTREACHED*/
407 }
408 }
409
410 return (status);
411}
412
413
414/*-
415 *-----------------------------------------------------------------------
416 * CompatMake --
417 * Make a target.
418 *
419 * Results:
420 * 0
421 *
422 * Side Effects:
423 * If an error is detected and not being ignored, the process exits.
424 *
425 *-----------------------------------------------------------------------
426 */
427static int
428CompatMake (gnp, pgnp)
429 ClientData gnp; /* The node to make */
430 ClientData pgnp; /* Parent to abort if necessary */
431{
432 GNode *gn = (GNode *) gnp;
433 GNode *pgn = (GNode *) pgnp;
434 if (gn->type & OP_USE) {
435 Make_HandleUse(gn, pgn);
436 } else if (gn->made == UNMADE) {
437 /*
438 * First mark ourselves to be made, then apply whatever transformations
439 * the suffix module thinks are necessary. Once that's done, we can
440 * descend and make all our children. If any of them has an error
441 * but the -k flag was given, our 'make' field will be set FALSE again.
442 * This is our signal to not attempt to do anything but abort our
443 * parent as well.
444 */
445 gn->make = TRUE;
446 gn->made = BEINGMADE;
447 Suff_FindDeps (gn);
448 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
449 if (!gn->make) {
450 gn->made = ABORTED;
451 pgn->make = FALSE;
452 return (0);
453 }
454
455 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
456 char *p1;
457 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
458 efree(p1);
459 }
460
461 /*
462 * All the children were made ok. Now cmtime contains the modification
463 * time of the newest child, we need to find out if we exist and when
464 * we were modified last. The criteria for datedness are defined by the
465 * Make_OODate function.
466 */
467 if (DEBUG(MAKE)) {
468 printf("Examining %s...", gn->name);
469 }
470 if (! Make_OODate(gn)) {
471 gn->made = UPTODATE;
472 if (DEBUG(MAKE)) {
473 printf("up-to-date.\n");
474 }
475 return (0);
476 } else if (DEBUG(MAKE)) {
477 printf("out-of-date.\n");
478 }
479
480 /*
481 * If the user is just seeing if something is out-of-date, exit now
482 * to tell him/her "yes".
483 */
484 if (queryFlag) {
485 exit (-1);
486 }
487
488 /*
489 * We need to be re-made. We also have to make sure we've got a $?
490 * variable. To be nice, we also define the $> variable using
491 * Make_DoAllVar().
492 */
493 Make_DoAllVar(gn);
494
495 /*
496 * Alter our type to tell if errors should be ignored or things
497 * should not be printed so CompatRunCommand knows what to do.
498 */
499 if (Targ_Ignore (gn)) {
500 gn->type |= OP_IGNORE;
501 }
502 if (Targ_Silent (gn)) {
503 gn->type |= OP_SILENT;
504 }
505
506 if (Job_CheckCommands (gn, Fatal)) {
507 /*
508 * Our commands are ok, but we still have to worry about the -t
509 * flag...
510 */
511 if (!touchFlag) {
512 curTarg = gn;
513 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
514 curTarg = NILGNODE;
515 } else {
516 Job_Touch (gn, gn->type & OP_SILENT);
517 }
518 } else {
519 gn->made = ERROR;
520 }
521
522 if (gn->made != ERROR) {
523 /*
524 * If the node was made successfully, mark it so, update
525 * its modification time and timestamp all its parents. Note
526 * that for .ZEROTIME targets, the timestamping isn't done.
527 * This is to keep its state from affecting that of its parent.
528 */
529 gn->made = MADE;
530#ifndef RECHECK
531 /*
532 * We can't re-stat the thing, but we can at least take care of
533 * rules where a target depends on a source that actually creates
534 * the target, but only if it has changed, e.g.
535 *
536 * parse.h : parse.o
537 *
538 * parse.o : parse.y
539 * yacc -d parse.y
540 * cc -c y.tab.c
541 * mv y.tab.o parse.o
542 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
543 *
544 * In this case, if the definitions produced by yacc haven't
545 * changed from before, parse.h won't have been updated and
546 * gn->mtime will reflect the current modification time for
547 * parse.h. This is something of a kludge, I admit, but it's a
548 * useful one..
549 *
550 * XXX: People like to use a rule like
551 *
552 * FRC:
553 *
554 * To force things that depend on FRC to be made, so we have to
555 * check for gn->children being empty as well...
556 */
557 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
558 gn->mtime = now;
559 }
560#else
561 /*
562 * This is what Make does and it's actually a good thing, as it
563 * allows rules like
564 *
565 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
566 *
567 * to function as intended. Unfortunately, thanks to the stateless
568 * nature of NFS (and the speed of this program), there are times
569 * when the modification time of a file created on a remote
570 * machine will not be modified before the stat() implied by
571 * the Dir_MTime occurs, thus leading us to believe that the file
572 * is unchanged, wreaking havoc with files that depend on this one.
573 *
574 * I have decided it is better to make too much than to make too
575 * little, so this stuff is commented out unless you're sure it's
576 * ok.
577 * -- ardeb 1/12/88
578 */
579 if (noExecute || Dir_MTime(gn) == 0) {
580 gn->mtime = now;
581 }
582 if (gn->cmtime > gn->mtime)
583 gn->mtime = gn->cmtime;
584 if (DEBUG(MAKE)) {
585 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
586 }
587#endif
588 if (!(gn->type & OP_EXEC)) {
589 pgn->childMade = TRUE;
590 Make_TimeStamp(pgn, gn);
591 }
592 } else if (keepgoing) {
593 pgn->make = FALSE;
594 } else {
595 char *p1;
596
597 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
598 efree(p1);
599 exit (1);
600 }
601 } else if (gn->made == ERROR) {
602 /*
603 * Already had an error when making this beastie. Tell the parent
604 * to abort.
605 */
606 pgn->make = FALSE;
607 } else {
608 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
609 char *p1;
610 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
611 efree(p1);
612 }
613 switch(gn->made) {
614 case BEINGMADE:
615 Error("Graph cycles through %s\n", gn->name);
616 gn->made = ERROR;
617 pgn->make = FALSE;
618 break;
619 case MADE:
620 if ((gn->type & OP_EXEC) == 0) {
621 pgn->childMade = TRUE;
622 Make_TimeStamp(pgn, gn);
623 }
624 break;
625 case UPTODATE:
626 if ((gn->type & OP_EXEC) == 0) {
627 Make_TimeStamp(pgn, gn);
628 }
629 break;
630 default:
631 break;
632 }
633 }
634
635 return (0);
636}
637
638
639/*-
640 *-----------------------------------------------------------------------
641 * Compat_Run --
642 * Initialize this mode and start making.
643 *
644 * Results:
645 * None.
646 *
647 * Side Effects:
648 * Guess what?
649 *
650 *-----------------------------------------------------------------------
651 */
652void
653Compat_Run(targs)
654 Lst targs; /* List of target nodes to re-create */
655{
656 char *cp; /* Pointer to string of shell meta-characters */
657 GNode *gn = NULL;/* Current root target */
658 int errors; /* Number of targets not remade due to errors */
659
660 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
661 signal(SIGINT, CompatInterrupt);
662 }
663 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
664 signal(SIGTERM, CompatInterrupt);
665 }
666 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
667 signal(SIGHUP, CompatInterrupt);
668 }
669 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
670 signal(SIGQUIT, CompatInterrupt);
671 }
672
673 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
674 meta[(unsigned char) *cp] = 1;
675 }
676 /*
677 * The null character serves as a sentinel in the string.
678 */
679 meta[0] = 1;
680
681 ENDNode = Targ_FindNode(".END", TARG_CREATE);
682 /*
683 * If the user has defined a .BEGIN target, execute the commands attached
684 * to it.
685 */
686 if (!queryFlag) {
687 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
688 if (gn != NILGNODE) {
689 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
690 if (gn->made == ERROR) {
691 printf("\n\nStop.\n");
692 exit(1);
693 }
694 }
695 }
696
697 /*
698 * For each entry in the list of targets to create, call CompatMake on
699 * it to create the thing. CompatMake will leave the 'made' field of gn
700 * in one of several states:
701 * UPTODATE gn was already up-to-date
702 * MADE gn was recreated successfully
703 * ERROR An error occurred while gn was being created
704 * ABORTED gn was not remade because one of its inferiors
705 * could not be made due to errors.
706 */
707 errors = 0;
708 while (!Lst_IsEmpty (targs)) {
709 gn = (GNode *) Lst_DeQueue (targs);
710 CompatMake (gn, gn);
711
712 if (gn->made == UPTODATE) {
713 printf ("`%s' is up to date.\n", gn->name);
714 } else if (gn->made == ABORTED) {
715 printf ("`%s' not remade because of errors.\n", gn->name);
716 errors += 1;
717 }
718 }
719
720 /*
721 * If the user has defined a .END target, run its commands.
722 */
723 if (errors == 0) {
724 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
725 }
726}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette