xref: /netbsd-src/external/gpl2/gmake/dist/commands.c (revision 69606e3f5c9388e52aed8c120ad63c049ca45d8f)
1*69606e3fSchristos /* Command processing for GNU Make.
2*69606e3fSchristos Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3*69606e3fSchristos 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4*69606e3fSchristos Foundation, Inc.
5*69606e3fSchristos This file is part of GNU Make.
6*69606e3fSchristos 
7*69606e3fSchristos GNU Make is free software; you can redistribute it and/or modify it under the
8*69606e3fSchristos terms of the GNU General Public License as published by the Free Software
9*69606e3fSchristos Foundation; either version 2, or (at your option) any later version.
10*69606e3fSchristos 
11*69606e3fSchristos GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12*69606e3fSchristos WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13*69606e3fSchristos A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14*69606e3fSchristos 
15*69606e3fSchristos You should have received a copy of the GNU General Public License along with
16*69606e3fSchristos GNU Make; see the file COPYING.  If not, write to the Free Software
17*69606e3fSchristos Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
18*69606e3fSchristos 
19*69606e3fSchristos #include "make.h"
20*69606e3fSchristos #include "dep.h"
21*69606e3fSchristos #include "filedef.h"
22*69606e3fSchristos #include "variable.h"
23*69606e3fSchristos #include "job.h"
24*69606e3fSchristos #include "commands.h"
25*69606e3fSchristos #ifdef WINDOWS32
26*69606e3fSchristos #include <windows.h>
27*69606e3fSchristos #include "w32err.h"
28*69606e3fSchristos #endif
29*69606e3fSchristos 
30*69606e3fSchristos #if VMS
31*69606e3fSchristos # define FILE_LIST_SEPARATOR ','
32*69606e3fSchristos #else
33*69606e3fSchristos # define FILE_LIST_SEPARATOR ' '
34*69606e3fSchristos #endif
35*69606e3fSchristos 
36*69606e3fSchristos extern int remote_kill PARAMS ((int id, int sig));
37*69606e3fSchristos 
38*69606e3fSchristos #ifndef	HAVE_UNISTD_H
39*69606e3fSchristos extern int getpid ();
40*69606e3fSchristos #endif
41*69606e3fSchristos 
42*69606e3fSchristos /* Set FILE's automatic variables up.  */
43*69606e3fSchristos 
44*69606e3fSchristos void
set_file_variables(struct file * file)45*69606e3fSchristos set_file_variables (struct file *file)
46*69606e3fSchristos {
47*69606e3fSchristos   struct dep *d;
48*69606e3fSchristos   char *at, *percent, *star, *less;
49*69606e3fSchristos 
50*69606e3fSchristos #ifndef	NO_ARCHIVES
51*69606e3fSchristos   /* If the target is an archive member `lib(member)',
52*69606e3fSchristos      then $@ is `lib' and $% is `member'.  */
53*69606e3fSchristos 
54*69606e3fSchristos   if (ar_name (file->name))
55*69606e3fSchristos     {
56*69606e3fSchristos       unsigned int len;
57*69606e3fSchristos       char *p;
58*69606e3fSchristos 
59*69606e3fSchristos       p = strchr (file->name, '(');
60*69606e3fSchristos       at = (char *) alloca (p - file->name + 1);
61*69606e3fSchristos       bcopy (file->name, at, p - file->name);
62*69606e3fSchristos       at[p - file->name] = '\0';
63*69606e3fSchristos       len = strlen (p + 1);
64*69606e3fSchristos       percent = (char *) alloca (len);
65*69606e3fSchristos       bcopy (p + 1, percent, len - 1);
66*69606e3fSchristos       percent[len - 1] = '\0';
67*69606e3fSchristos     }
68*69606e3fSchristos   else
69*69606e3fSchristos #endif	/* NO_ARCHIVES.  */
70*69606e3fSchristos     {
71*69606e3fSchristos       at = file->name;
72*69606e3fSchristos       percent = "";
73*69606e3fSchristos     }
74*69606e3fSchristos 
75*69606e3fSchristos   /* $* is the stem from an implicit or static pattern rule.  */
76*69606e3fSchristos   if (file->stem == 0)
77*69606e3fSchristos     {
78*69606e3fSchristos       /* In Unix make, $* is set to the target name with
79*69606e3fSchristos 	 any suffix in the .SUFFIXES list stripped off for
80*69606e3fSchristos 	 explicit rules.  We store this in the `stem' member.  */
81*69606e3fSchristos       register struct dep *d;
82*69606e3fSchristos       char *name;
83*69606e3fSchristos       unsigned int len;
84*69606e3fSchristos 
85*69606e3fSchristos #ifndef	NO_ARCHIVES
86*69606e3fSchristos       if (ar_name (file->name))
87*69606e3fSchristos 	{
88*69606e3fSchristos 	  name = strchr (file->name, '(') + 1;
89*69606e3fSchristos 	  len = strlen (name) - 1;
90*69606e3fSchristos 	}
91*69606e3fSchristos       else
92*69606e3fSchristos #endif
93*69606e3fSchristos 	{
94*69606e3fSchristos 	  name = file->name;
95*69606e3fSchristos 	  len = strlen (name);
96*69606e3fSchristos 	}
97*69606e3fSchristos 
98*69606e3fSchristos       for (d = enter_file (".SUFFIXES")->deps; d != 0; d = d->next)
99*69606e3fSchristos 	{
100*69606e3fSchristos 	  unsigned int slen = strlen (dep_name (d));
101*69606e3fSchristos 	  if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
102*69606e3fSchristos 	    {
103*69606e3fSchristos 	      file->stem = savestring (name, len - slen);
104*69606e3fSchristos 	      break;
105*69606e3fSchristos 	    }
106*69606e3fSchristos 	}
107*69606e3fSchristos       if (d == 0)
108*69606e3fSchristos 	file->stem = "";
109*69606e3fSchristos     }
110*69606e3fSchristos   star = file->stem;
111*69606e3fSchristos 
112*69606e3fSchristos   /* $< is the first not order-only dependency.  */
113*69606e3fSchristos   less = "";
114*69606e3fSchristos   for (d = file->deps; d != 0; d = d->next)
115*69606e3fSchristos     if (!d->ignore_mtime)
116*69606e3fSchristos       {
117*69606e3fSchristos         less = dep_name (d);
118*69606e3fSchristos         break;
119*69606e3fSchristos       }
120*69606e3fSchristos 
121*69606e3fSchristos   if (file->cmds == default_file->cmds)
122*69606e3fSchristos     /* This file got its commands from .DEFAULT.
123*69606e3fSchristos        In this case $< is the same as $@.  */
124*69606e3fSchristos     less = at;
125*69606e3fSchristos 
126*69606e3fSchristos #define	DEFINE_VARIABLE(name, len, value) \
127*69606e3fSchristos   (void) define_variable_for_file (name,len,value,o_automatic,0,file)
128*69606e3fSchristos 
129*69606e3fSchristos   /* Define the variables.  */
130*69606e3fSchristos 
131*69606e3fSchristos   DEFINE_VARIABLE ("<", 1, less);
132*69606e3fSchristos   DEFINE_VARIABLE ("*", 1, star);
133*69606e3fSchristos   DEFINE_VARIABLE ("@", 1, at);
134*69606e3fSchristos   DEFINE_VARIABLE ("%", 1, percent);
135*69606e3fSchristos 
136*69606e3fSchristos   /* Compute the values for $^, $+, $?, and $|.  */
137*69606e3fSchristos 
138*69606e3fSchristos   {
139*69606e3fSchristos     static char *plus_value=0, *bar_value=0, *qmark_value=0;
140*69606e3fSchristos     static unsigned int qmark_max=0, plus_max=0, bar_max=0;
141*69606e3fSchristos 
142*69606e3fSchristos     unsigned int qmark_len, plus_len, bar_len;
143*69606e3fSchristos     char *cp;
144*69606e3fSchristos     char *caret_value;
145*69606e3fSchristos     char *qp;
146*69606e3fSchristos     char *bp;
147*69606e3fSchristos     unsigned int len;
148*69606e3fSchristos 
149*69606e3fSchristos     /* Compute first the value for $+, which is supposed to contain
150*69606e3fSchristos        duplicate dependencies as they were listed in the makefile.  */
151*69606e3fSchristos 
152*69606e3fSchristos     plus_len = 0;
153*69606e3fSchristos     for (d = file->deps; d != 0; d = d->next)
154*69606e3fSchristos       if (! d->ignore_mtime)
155*69606e3fSchristos 	plus_len += strlen (dep_name (d)) + 1;
156*69606e3fSchristos     if (plus_len == 0)
157*69606e3fSchristos       plus_len++;
158*69606e3fSchristos 
159*69606e3fSchristos     if (plus_len > plus_max)
160*69606e3fSchristos       plus_value = xrealloc (plus_value, plus_max = plus_len);
161*69606e3fSchristos     cp = plus_value;
162*69606e3fSchristos 
163*69606e3fSchristos     qmark_len = plus_len + 1;	/* Will be this or less.  */
164*69606e3fSchristos     for (d = file->deps; d != 0; d = d->next)
165*69606e3fSchristos       if (! d->ignore_mtime)
166*69606e3fSchristos         {
167*69606e3fSchristos           char *c = dep_name (d);
168*69606e3fSchristos 
169*69606e3fSchristos #ifndef	NO_ARCHIVES
170*69606e3fSchristos           if (ar_name (c))
171*69606e3fSchristos             {
172*69606e3fSchristos               c = strchr (c, '(') + 1;
173*69606e3fSchristos               len = strlen (c) - 1;
174*69606e3fSchristos             }
175*69606e3fSchristos           else
176*69606e3fSchristos #endif
177*69606e3fSchristos             len = strlen (c);
178*69606e3fSchristos 
179*69606e3fSchristos           bcopy (c, cp, len);
180*69606e3fSchristos           cp += len;
181*69606e3fSchristos           *cp++ = FILE_LIST_SEPARATOR;
182*69606e3fSchristos           if (! d->changed)
183*69606e3fSchristos             qmark_len -= len + 1;	/* Don't space in $? for this one.  */
184*69606e3fSchristos         }
185*69606e3fSchristos 
186*69606e3fSchristos     /* Kill the last space and define the variable.  */
187*69606e3fSchristos 
188*69606e3fSchristos     cp[cp > plus_value ? -1 : 0] = '\0';
189*69606e3fSchristos     DEFINE_VARIABLE ("+", 1, plus_value);
190*69606e3fSchristos 
191*69606e3fSchristos     /* Make sure that no dependencies are repeated.  This does not
192*69606e3fSchristos        really matter for the purpose of updating targets, but it
193*69606e3fSchristos        might make some names be listed twice for $^ and $?.  */
194*69606e3fSchristos 
195*69606e3fSchristos     uniquize_deps (file->deps);
196*69606e3fSchristos 
197*69606e3fSchristos     bar_len = 0;
198*69606e3fSchristos     for (d = file->deps; d != 0; d = d->next)
199*69606e3fSchristos       if (d->ignore_mtime)
200*69606e3fSchristos 	bar_len += strlen (dep_name (d)) + 1;
201*69606e3fSchristos     if (bar_len == 0)
202*69606e3fSchristos       bar_len++;
203*69606e3fSchristos 
204*69606e3fSchristos     /* Compute the values for $^, $?, and $|.  */
205*69606e3fSchristos 
206*69606e3fSchristos     cp = caret_value = plus_value; /* Reuse the buffer; it's big enough.  */
207*69606e3fSchristos 
208*69606e3fSchristos     if (qmark_len > qmark_max)
209*69606e3fSchristos       qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
210*69606e3fSchristos     qp = qmark_value;
211*69606e3fSchristos 
212*69606e3fSchristos     if (bar_len > bar_max)
213*69606e3fSchristos       bar_value = xrealloc (bar_value, bar_max = bar_len);
214*69606e3fSchristos     bp = bar_value;
215*69606e3fSchristos 
216*69606e3fSchristos     for (d = file->deps; d != 0; d = d->next)
217*69606e3fSchristos       {
218*69606e3fSchristos 	char *c = dep_name (d);
219*69606e3fSchristos 
220*69606e3fSchristos #ifndef	NO_ARCHIVES
221*69606e3fSchristos 	if (ar_name (c))
222*69606e3fSchristos 	  {
223*69606e3fSchristos 	    c = strchr (c, '(') + 1;
224*69606e3fSchristos 	    len = strlen (c) - 1;
225*69606e3fSchristos 	  }
226*69606e3fSchristos 	else
227*69606e3fSchristos #endif
228*69606e3fSchristos 	  len = strlen (c);
229*69606e3fSchristos 
230*69606e3fSchristos         if (d->ignore_mtime)
231*69606e3fSchristos           {
232*69606e3fSchristos 	    bcopy (c, bp, len);
233*69606e3fSchristos 	    bp += len;
234*69606e3fSchristos 	    *bp++ = FILE_LIST_SEPARATOR;
235*69606e3fSchristos 	  }
236*69606e3fSchristos 	else
237*69606e3fSchristos 	  {
238*69606e3fSchristos             bcopy (c, cp, len);
239*69606e3fSchristos             cp += len;
240*69606e3fSchristos             *cp++ = FILE_LIST_SEPARATOR;
241*69606e3fSchristos             if (d->changed)
242*69606e3fSchristos               {
243*69606e3fSchristos                 bcopy (c, qp, len);
244*69606e3fSchristos                 qp += len;
245*69606e3fSchristos                 *qp++ = FILE_LIST_SEPARATOR;
246*69606e3fSchristos               }
247*69606e3fSchristos           }
248*69606e3fSchristos       }
249*69606e3fSchristos 
250*69606e3fSchristos     /* Kill the last spaces and define the variables.  */
251*69606e3fSchristos 
252*69606e3fSchristos     cp[cp > caret_value ? -1 : 0] = '\0';
253*69606e3fSchristos     DEFINE_VARIABLE ("^", 1, caret_value);
254*69606e3fSchristos 
255*69606e3fSchristos     qp[qp > qmark_value ? -1 : 0] = '\0';
256*69606e3fSchristos     DEFINE_VARIABLE ("?", 1, qmark_value);
257*69606e3fSchristos 
258*69606e3fSchristos     bp[bp > bar_value ? -1 : 0] = '\0';
259*69606e3fSchristos     DEFINE_VARIABLE ("|", 1, bar_value);
260*69606e3fSchristos   }
261*69606e3fSchristos 
262*69606e3fSchristos #undef	DEFINE_VARIABLE
263*69606e3fSchristos }
264*69606e3fSchristos 
265*69606e3fSchristos /* Chop CMDS up into individual command lines if necessary.
266*69606e3fSchristos    Also set the `lines_flags' and `any_recurse' members.  */
267*69606e3fSchristos 
268*69606e3fSchristos void
chop_commands(struct commands * cmds)269*69606e3fSchristos chop_commands (struct commands *cmds)
270*69606e3fSchristos {
271*69606e3fSchristos   register char *p;
272*69606e3fSchristos   unsigned int nlines, idx;
273*69606e3fSchristos   char **lines;
274*69606e3fSchristos 
275*69606e3fSchristos   /* If we don't have any commands,
276*69606e3fSchristos      or we already parsed them, never mind.  */
277*69606e3fSchristos 
278*69606e3fSchristos   if (!cmds || cmds->command_lines != 0)
279*69606e3fSchristos     return;
280*69606e3fSchristos 
281*69606e3fSchristos   /* Chop CMDS->commands up into lines in CMDS->command_lines.
282*69606e3fSchristos 	 Also set the corresponding CMDS->lines_flags elements,
283*69606e3fSchristos 	 and the CMDS->any_recurse flag.  */
284*69606e3fSchristos 
285*69606e3fSchristos   nlines = 5;
286*69606e3fSchristos   lines = (char **) xmalloc (5 * sizeof (char *));
287*69606e3fSchristos   idx = 0;
288*69606e3fSchristos   p = cmds->commands;
289*69606e3fSchristos   while (*p != '\0')
290*69606e3fSchristos     {
291*69606e3fSchristos       char *end = p;
292*69606e3fSchristos     find_end:;
293*69606e3fSchristos       end = strchr (end, '\n');
294*69606e3fSchristos       if (end == 0)
295*69606e3fSchristos         end = p + strlen (p);
296*69606e3fSchristos       else if (end > p && end[-1] == '\\')
297*69606e3fSchristos         {
298*69606e3fSchristos           int backslash = 1;
299*69606e3fSchristos           register char *b;
300*69606e3fSchristos           for (b = end - 2; b >= p && *b == '\\'; --b)
301*69606e3fSchristos             backslash = !backslash;
302*69606e3fSchristos           if (backslash)
303*69606e3fSchristos             {
304*69606e3fSchristos               ++end;
305*69606e3fSchristos               goto find_end;
306*69606e3fSchristos             }
307*69606e3fSchristos         }
308*69606e3fSchristos 
309*69606e3fSchristos       if (idx == nlines)
310*69606e3fSchristos         {
311*69606e3fSchristos           nlines += 2;
312*69606e3fSchristos           lines = (char **) xrealloc ((char *) lines,
313*69606e3fSchristos                                       nlines * sizeof (char *));
314*69606e3fSchristos         }
315*69606e3fSchristos       lines[idx++] = savestring (p, end - p);
316*69606e3fSchristos       p = end;
317*69606e3fSchristos       if (*p != '\0')
318*69606e3fSchristos         ++p;
319*69606e3fSchristos     }
320*69606e3fSchristos 
321*69606e3fSchristos   if (idx != nlines)
322*69606e3fSchristos     {
323*69606e3fSchristos       nlines = idx;
324*69606e3fSchristos       lines = (char **) xrealloc ((char *) lines,
325*69606e3fSchristos                                   nlines * sizeof (char *));
326*69606e3fSchristos     }
327*69606e3fSchristos 
328*69606e3fSchristos   cmds->ncommand_lines = nlines;
329*69606e3fSchristos   cmds->command_lines = lines;
330*69606e3fSchristos 
331*69606e3fSchristos   cmds->any_recurse = 0;
332*69606e3fSchristos   cmds->lines_flags = (char *) xmalloc (nlines);
333*69606e3fSchristos   for (idx = 0; idx < nlines; ++idx)
334*69606e3fSchristos     {
335*69606e3fSchristos       int flags = 0;
336*69606e3fSchristos 
337*69606e3fSchristos       for (p = lines[idx];
338*69606e3fSchristos            isblank ((unsigned char)*p) || *p == '-' || *p == '@' || *p == '+';
339*69606e3fSchristos            ++p)
340*69606e3fSchristos         switch (*p)
341*69606e3fSchristos           {
342*69606e3fSchristos           case '+':
343*69606e3fSchristos             flags |= COMMANDS_RECURSE;
344*69606e3fSchristos             break;
345*69606e3fSchristos           case '@':
346*69606e3fSchristos             flags |= COMMANDS_SILENT;
347*69606e3fSchristos             break;
348*69606e3fSchristos           case '-':
349*69606e3fSchristos             flags |= COMMANDS_NOERROR;
350*69606e3fSchristos             break;
351*69606e3fSchristos           }
352*69606e3fSchristos 
353*69606e3fSchristos       /* If no explicit '+' was given, look for MAKE variable references.  */
354*69606e3fSchristos       if (!(flags & COMMANDS_RECURSE)
355*69606e3fSchristos           && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
356*69606e3fSchristos         flags |= COMMANDS_RECURSE;
357*69606e3fSchristos 
358*69606e3fSchristos       cmds->lines_flags[idx] = flags;
359*69606e3fSchristos       cmds->any_recurse |= flags & COMMANDS_RECURSE;
360*69606e3fSchristos     }
361*69606e3fSchristos }
362*69606e3fSchristos 
363*69606e3fSchristos /* Execute the commands to remake FILE.  If they are currently executing,
364*69606e3fSchristos    return or have already finished executing, just return.  Otherwise,
365*69606e3fSchristos    fork off a child process to run the first command line in the sequence.  */
366*69606e3fSchristos 
367*69606e3fSchristos void
execute_file_commands(struct file * file)368*69606e3fSchristos execute_file_commands (struct file *file)
369*69606e3fSchristos {
370*69606e3fSchristos   register char *p;
371*69606e3fSchristos 
372*69606e3fSchristos   /* Don't go through all the preparations if
373*69606e3fSchristos      the commands are nothing but whitespace.  */
374*69606e3fSchristos 
375*69606e3fSchristos   for (p = file->cmds->commands; *p != '\0'; ++p)
376*69606e3fSchristos     if (!isspace ((unsigned char)*p) && *p != '-' && *p != '@')
377*69606e3fSchristos       break;
378*69606e3fSchristos   if (*p == '\0')
379*69606e3fSchristos     {
380*69606e3fSchristos       /* If there are no commands, assume everything worked.  */
381*69606e3fSchristos       set_command_state (file, cs_running);
382*69606e3fSchristos       file->update_status = 0;
383*69606e3fSchristos       notice_finished_file (file);
384*69606e3fSchristos       return;
385*69606e3fSchristos     }
386*69606e3fSchristos 
387*69606e3fSchristos   /* First set the automatic variables according to this file.  */
388*69606e3fSchristos 
389*69606e3fSchristos   initialize_file_variables (file, 0);
390*69606e3fSchristos 
391*69606e3fSchristos   set_file_variables (file);
392*69606e3fSchristos 
393*69606e3fSchristos   /* Start the commands running.  */
394*69606e3fSchristos   new_job (file);
395*69606e3fSchristos }
396*69606e3fSchristos 
397*69606e3fSchristos /* This is set while we are inside fatal_error_signal,
398*69606e3fSchristos    so things can avoid nonreentrant operations.  */
399*69606e3fSchristos 
400*69606e3fSchristos int handling_fatal_signal = 0;
401*69606e3fSchristos 
402*69606e3fSchristos /* Handle fatal signals.  */
403*69606e3fSchristos 
404*69606e3fSchristos RETSIGTYPE
fatal_error_signal(int sig)405*69606e3fSchristos fatal_error_signal (int sig)
406*69606e3fSchristos {
407*69606e3fSchristos #ifdef __MSDOS__
408*69606e3fSchristos   extern int dos_status, dos_command_running;
409*69606e3fSchristos 
410*69606e3fSchristos   if (dos_command_running)
411*69606e3fSchristos     {
412*69606e3fSchristos       /* That was the child who got the signal, not us.  */
413*69606e3fSchristos       dos_status |= (sig << 8);
414*69606e3fSchristos       return;
415*69606e3fSchristos     }
416*69606e3fSchristos   remove_intermediates (1);
417*69606e3fSchristos   exit (EXIT_FAILURE);
418*69606e3fSchristos #else /* not __MSDOS__ */
419*69606e3fSchristos #ifdef _AMIGA
420*69606e3fSchristos   remove_intermediates (1);
421*69606e3fSchristos   if (sig == SIGINT)
422*69606e3fSchristos      fputs (_("*** Break.\n"), stderr);
423*69606e3fSchristos 
424*69606e3fSchristos   exit (10);
425*69606e3fSchristos #else /* not Amiga */
426*69606e3fSchristos #ifdef WINDOWS32
427*69606e3fSchristos   extern HANDLE main_thread;
428*69606e3fSchristos 
429*69606e3fSchristos   /* Windows creates a sperate thread for handling Ctrl+C, so we need
430*69606e3fSchristos      to suspend the main thread, or else we will have race conditions
431*69606e3fSchristos      when both threads call reap_children.  */
432*69606e3fSchristos   if (main_thread)
433*69606e3fSchristos     {
434*69606e3fSchristos       DWORD susp_count = SuspendThread (main_thread);
435*69606e3fSchristos 
436*69606e3fSchristos       if (susp_count != 0)
437*69606e3fSchristos 	fprintf (stderr, "SuspendThread: suspend count = %ld\n", susp_count);
438*69606e3fSchristos       else if (susp_count == (DWORD)-1)
439*69606e3fSchristos 	{
440*69606e3fSchristos 	  DWORD ierr = GetLastError ();
441*69606e3fSchristos 
442*69606e3fSchristos 	  fprintf (stderr, "SuspendThread: error %ld: %s\n",
443*69606e3fSchristos 		   ierr, map_windows32_error_to_string (ierr));
444*69606e3fSchristos 	}
445*69606e3fSchristos     }
446*69606e3fSchristos #endif
447*69606e3fSchristos   handling_fatal_signal = 1;
448*69606e3fSchristos 
449*69606e3fSchristos   /* Set the handling for this signal to the default.
450*69606e3fSchristos      It is blocked now while we run this handler.  */
451*69606e3fSchristos   signal (sig, SIG_DFL);
452*69606e3fSchristos 
453*69606e3fSchristos   /* A termination signal won't be sent to the entire
454*69606e3fSchristos      process group, but it means we want to kill the children.  */
455*69606e3fSchristos 
456*69606e3fSchristos   if (sig == SIGTERM)
457*69606e3fSchristos     {
458*69606e3fSchristos       register struct child *c;
459*69606e3fSchristos       for (c = children; c != 0; c = c->next)
460*69606e3fSchristos 	if (!c->remote)
461*69606e3fSchristos 	  (void) kill (c->pid, SIGTERM);
462*69606e3fSchristos     }
463*69606e3fSchristos 
464*69606e3fSchristos   /* If we got a signal that means the user
465*69606e3fSchristos      wanted to kill make, remove pending targets.  */
466*69606e3fSchristos 
467*69606e3fSchristos   if (sig == SIGTERM || sig == SIGINT
468*69606e3fSchristos #ifdef SIGHUP
469*69606e3fSchristos     || sig == SIGHUP
470*69606e3fSchristos #endif
471*69606e3fSchristos #ifdef SIGQUIT
472*69606e3fSchristos     || sig == SIGQUIT
473*69606e3fSchristos #endif
474*69606e3fSchristos     )
475*69606e3fSchristos     {
476*69606e3fSchristos       register struct child *c;
477*69606e3fSchristos 
478*69606e3fSchristos       /* Remote children won't automatically get signals sent
479*69606e3fSchristos 	 to the process group, so we must send them.  */
480*69606e3fSchristos       for (c = children; c != 0; c = c->next)
481*69606e3fSchristos 	if (c->remote)
482*69606e3fSchristos 	  (void) remote_kill (c->pid, sig);
483*69606e3fSchristos 
484*69606e3fSchristos       for (c = children; c != 0; c = c->next)
485*69606e3fSchristos 	delete_child_targets (c);
486*69606e3fSchristos 
487*69606e3fSchristos       /* Clean up the children.  We don't just use the call below because
488*69606e3fSchristos 	 we don't want to print the "Waiting for children" message.  */
489*69606e3fSchristos       while (job_slots_used > 0)
490*69606e3fSchristos 	reap_children (1, 0);
491*69606e3fSchristos     }
492*69606e3fSchristos   else
493*69606e3fSchristos     /* Wait for our children to die.  */
494*69606e3fSchristos     while (job_slots_used > 0)
495*69606e3fSchristos       reap_children (1, 1);
496*69606e3fSchristos 
497*69606e3fSchristos   /* Delete any non-precious intermediate files that were made.  */
498*69606e3fSchristos 
499*69606e3fSchristos   remove_intermediates (1);
500*69606e3fSchristos 
501*69606e3fSchristos #ifdef SIGQUIT
502*69606e3fSchristos   if (sig == SIGQUIT)
503*69606e3fSchristos     /* We don't want to send ourselves SIGQUIT, because it will
504*69606e3fSchristos        cause a core dump.  Just exit instead.  */
505*69606e3fSchristos     exit (EXIT_FAILURE);
506*69606e3fSchristos #endif
507*69606e3fSchristos 
508*69606e3fSchristos #ifdef WINDOWS32
509*69606e3fSchristos   if (main_thread)
510*69606e3fSchristos     CloseHandle (main_thread);
511*69606e3fSchristos   /* Cannot call W32_kill with a pid (it needs a handle).  The exit
512*69606e3fSchristos      status of 130 emulates what happens in Bash.  */
513*69606e3fSchristos   exit (130);
514*69606e3fSchristos #else
515*69606e3fSchristos   /* Signal the same code; this time it will really be fatal.  The signal
516*69606e3fSchristos      will be unblocked when we return and arrive then to kill us.  */
517*69606e3fSchristos   if (kill (getpid (), sig) < 0)
518*69606e3fSchristos     pfatal_with_name ("kill");
519*69606e3fSchristos #endif /* not WINDOWS32 */
520*69606e3fSchristos #endif /* not Amiga */
521*69606e3fSchristos #endif /* not __MSDOS__  */
522*69606e3fSchristos }
523*69606e3fSchristos 
524*69606e3fSchristos /* Delete FILE unless it's precious or not actually a file (phony),
525*69606e3fSchristos    and it has changed on disk since we last stat'd it.  */
526*69606e3fSchristos 
527*69606e3fSchristos static void
delete_target(struct file * file,char * on_behalf_of)528*69606e3fSchristos delete_target (struct file *file, char *on_behalf_of)
529*69606e3fSchristos {
530*69606e3fSchristos   struct stat st;
531*69606e3fSchristos   int e;
532*69606e3fSchristos 
533*69606e3fSchristos   if (file->precious || file->phony)
534*69606e3fSchristos     return;
535*69606e3fSchristos 
536*69606e3fSchristos #ifndef NO_ARCHIVES
537*69606e3fSchristos   if (ar_name (file->name))
538*69606e3fSchristos     {
539*69606e3fSchristos       time_t file_date = (file->last_mtime == NONEXISTENT_MTIME
540*69606e3fSchristos 			  ? (time_t) -1
541*69606e3fSchristos 			  : (time_t) FILE_TIMESTAMP_S (file->last_mtime));
542*69606e3fSchristos       if (ar_member_date (file->name) != file_date)
543*69606e3fSchristos 	{
544*69606e3fSchristos 	  if (on_behalf_of)
545*69606e3fSchristos 	    error (NILF, _("*** [%s] Archive member `%s' may be bogus; not deleted"),
546*69606e3fSchristos 		   on_behalf_of, file->name);
547*69606e3fSchristos 	  else
548*69606e3fSchristos 	    error (NILF, _("*** Archive member `%s' may be bogus; not deleted"),
549*69606e3fSchristos 		   file->name);
550*69606e3fSchristos 	}
551*69606e3fSchristos       return;
552*69606e3fSchristos     }
553*69606e3fSchristos #endif
554*69606e3fSchristos 
555*69606e3fSchristos   EINTRLOOP (e, stat (file->name, &st));
556*69606e3fSchristos   if (e == 0
557*69606e3fSchristos       && S_ISREG (st.st_mode)
558*69606e3fSchristos       && FILE_TIMESTAMP_STAT_MODTIME (file->name, st) != file->last_mtime)
559*69606e3fSchristos     {
560*69606e3fSchristos       if (on_behalf_of)
561*69606e3fSchristos 	error (NILF, _("*** [%s] Deleting file `%s'"), on_behalf_of, file->name);
562*69606e3fSchristos       else
563*69606e3fSchristos 	error (NILF, _("*** Deleting file `%s'"), file->name);
564*69606e3fSchristos       if (unlink (file->name) < 0
565*69606e3fSchristos 	  && errno != ENOENT)	/* It disappeared; so what.  */
566*69606e3fSchristos 	perror_with_name ("unlink: ", file->name);
567*69606e3fSchristos     }
568*69606e3fSchristos }
569*69606e3fSchristos 
570*69606e3fSchristos 
571*69606e3fSchristos /* Delete all non-precious targets of CHILD unless they were already deleted.
572*69606e3fSchristos    Set the flag in CHILD to say they've been deleted.  */
573*69606e3fSchristos 
574*69606e3fSchristos void
delete_child_targets(struct child * child)575*69606e3fSchristos delete_child_targets (struct child *child)
576*69606e3fSchristos {
577*69606e3fSchristos   struct dep *d;
578*69606e3fSchristos 
579*69606e3fSchristos   if (child->deleted)
580*69606e3fSchristos     return;
581*69606e3fSchristos 
582*69606e3fSchristos   /* Delete the target file if it changed.  */
583*69606e3fSchristos   delete_target (child->file, (char *) 0);
584*69606e3fSchristos 
585*69606e3fSchristos   /* Also remove any non-precious targets listed in the `also_make' member.  */
586*69606e3fSchristos   for (d = child->file->also_make; d != 0; d = d->next)
587*69606e3fSchristos     delete_target (d->file, child->file->name);
588*69606e3fSchristos 
589*69606e3fSchristos   child->deleted = 1;
590*69606e3fSchristos }
591*69606e3fSchristos 
592*69606e3fSchristos /* Print out the commands in CMDS.  */
593*69606e3fSchristos 
594*69606e3fSchristos void
print_commands(struct commands * cmds)595*69606e3fSchristos print_commands (struct commands *cmds)
596*69606e3fSchristos {
597*69606e3fSchristos   register char *s;
598*69606e3fSchristos 
599*69606e3fSchristos   fputs (_("#  commands to execute"), stdout);
600*69606e3fSchristos 
601*69606e3fSchristos   if (cmds->fileinfo.filenm == 0)
602*69606e3fSchristos     puts (_(" (built-in):"));
603*69606e3fSchristos   else
604*69606e3fSchristos     printf (_(" (from `%s', line %lu):\n"),
605*69606e3fSchristos             cmds->fileinfo.filenm, cmds->fileinfo.lineno);
606*69606e3fSchristos 
607*69606e3fSchristos   s = cmds->commands;
608*69606e3fSchristos   while (*s != '\0')
609*69606e3fSchristos     {
610*69606e3fSchristos       char *end;
611*69606e3fSchristos 
612*69606e3fSchristos       while (isspace ((unsigned char)*s))
613*69606e3fSchristos 	++s;
614*69606e3fSchristos 
615*69606e3fSchristos       end = strchr (s, '\n');
616*69606e3fSchristos       if (end == 0)
617*69606e3fSchristos 	end = s + strlen (s);
618*69606e3fSchristos 
619*69606e3fSchristos       printf ("\t%.*s\n", (int) (end - s), s);
620*69606e3fSchristos 
621*69606e3fSchristos       s = end;
622*69606e3fSchristos     }
623*69606e3fSchristos }
624