xref: /freebsd/contrib/bmake/meta.c (revision 226192822cddc30cacecd55bccb48f39c653058c)
18d5c8e21SSimon J. Gerraty /*      $NetBSD: meta.c,v 1.210 2024/06/02 15:31:26 rillig Exp $ */
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Implement 'meta' mode.
53955d011SMarcel Moolenaar  * Adapted from John Birrell's patches to FreeBSD make.
63955d011SMarcel Moolenaar  * --sjg
73955d011SMarcel Moolenaar  */
83955d011SMarcel Moolenaar /*
9be19d90bSSimon J. Gerraty  * Copyright (c) 2009-2016, Juniper Networks, Inc.
103955d011SMarcel Moolenaar  * Portions Copyright (c) 2009, John Birrell.
113955d011SMarcel Moolenaar  *
123955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
133955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
143955d011SMarcel Moolenaar  * are met:
153955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
173955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
183955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
193955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
203955d011SMarcel Moolenaar  *
213955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223955d011SMarcel Moolenaar  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233955d011SMarcel Moolenaar  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
243955d011SMarcel Moolenaar  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
253955d011SMarcel Moolenaar  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
263955d011SMarcel Moolenaar  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
273955d011SMarcel Moolenaar  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
283955d011SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
293955d011SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
303955d011SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
313955d011SMarcel Moolenaar  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
323955d011SMarcel Moolenaar  */
333955d011SMarcel Moolenaar #if defined(USE_META)
343955d011SMarcel Moolenaar 
353955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H
363955d011SMarcel Moolenaar # include "config.h"
373955d011SMarcel Moolenaar #endif
383955d011SMarcel Moolenaar #include <sys/stat.h>
39*22619282SSimon J. Gerraty #if defined(HAVE_SYS_SELECT_H)
40*22619282SSimon J. Gerraty # include <sys/select.h>
41*22619282SSimon J. Gerraty #endif
42ac3446e9SSimon J. Gerraty #ifdef HAVE_LIBGEN_H
433955d011SMarcel Moolenaar #include <libgen.h>
44ac3446e9SSimon J. Gerraty #elif !defined(HAVE_DIRNAME)
45ac3446e9SSimon J. Gerraty char * dirname(char *);
46ac3446e9SSimon J. Gerraty #endif
473955d011SMarcel Moolenaar #include <errno.h>
483955d011SMarcel Moolenaar #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
493955d011SMarcel Moolenaar #include <err.h>
503955d011SMarcel Moolenaar #endif
513955d011SMarcel Moolenaar 
523955d011SMarcel Moolenaar #include "make.h"
532c3632d1SSimon J. Gerraty #include "dir.h"
543955d011SMarcel Moolenaar #include "job.h"
553955d011SMarcel Moolenaar 
5649caa483SSimon J. Gerraty #ifdef USE_FILEMON
5749caa483SSimon J. Gerraty #include "filemon/filemon.h"
583955d011SMarcel Moolenaar #endif
593955d011SMarcel Moolenaar 
603955d011SMarcel Moolenaar static BuildMon Mybm;			/* for compat */
6106b9b3e0SSimon J. Gerraty static StringList metaBailiwick = LST_INIT; /* our scope of control */
62be19d90bSSimon J. Gerraty static char *metaBailiwickStr;		/* string storage for the list */
6306b9b3e0SSimon J. Gerraty static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
64be19d90bSSimon J. Gerraty static char *metaIgnorePathsStr;	/* string storage for the list */
6551ee2c1cSSimon J. Gerraty 
6651ee2c1cSSimon J. Gerraty #ifndef MAKE_META_IGNORE_PATHS
6751ee2c1cSSimon J. Gerraty #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
6851ee2c1cSSimon J. Gerraty #endif
69e48f47ddSSimon J. Gerraty #ifndef MAKE_META_IGNORE_PATTERNS
70e48f47ddSSimon J. Gerraty #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
71e48f47ddSSimon J. Gerraty #endif
7295e3ed2cSSimon J. Gerraty #ifndef MAKE_META_IGNORE_FILTER
7395e3ed2cSSimon J. Gerraty #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
7495e3ed2cSSimon J. Gerraty #endif
759f45a3c8SSimon J. Gerraty #ifndef MAKE_META_CMP_FILTER
769f45a3c8SSimon J. Gerraty #define MAKE_META_CMP_FILTER ".MAKE.META.CMP_FILTER"
779f45a3c8SSimon J. Gerraty #endif
783955d011SMarcel Moolenaar 
79b0c40a00SSimon J. Gerraty bool useMeta = false;
80b0c40a00SSimon J. Gerraty static bool useFilemon = false;
81b0c40a00SSimon J. Gerraty static bool writeMeta = false;
82b0c40a00SSimon J. Gerraty static bool metaMissing = false;	/* oodate if missing */
83b0c40a00SSimon J. Gerraty static bool filemonMissing = false;	/* oodate if missing */
84b0c40a00SSimon J. Gerraty static bool metaEnv = false;		/* don't save env unless asked */
85b0c40a00SSimon J. Gerraty static bool metaVerbose = false;
86b0c40a00SSimon J. Gerraty static bool metaIgnoreCMDs = false;	/* ignore CMDs in .meta files */
87b0c40a00SSimon J. Gerraty static bool metaIgnorePatterns = false; /* do we need to do pattern matches */
88b0c40a00SSimon J. Gerraty static bool metaIgnoreFilter = false;	/* do we have more complex filtering? */
899f45a3c8SSimon J. Gerraty static bool metaCmpFilter = false;	/* do we have CMP_FILTER ? */
90b0c40a00SSimon J. Gerraty static bool metaCurdirOk = false;	/* write .meta in .CURDIR Ok? */
91b0c40a00SSimon J. Gerraty static bool metaSilent = false;		/* if we have a .meta be SILENT */
923955d011SMarcel Moolenaar 
933955d011SMarcel Moolenaar 
943955d011SMarcel Moolenaar #define MAKE_META_PREFIX	".MAKE.META.PREFIX"
953955d011SMarcel Moolenaar 
963955d011SMarcel Moolenaar #ifndef N2U
973955d011SMarcel Moolenaar # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
983955d011SMarcel Moolenaar #endif
993955d011SMarcel Moolenaar #ifndef ROUNDUP
1003955d011SMarcel Moolenaar # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
1013955d011SMarcel Moolenaar #endif
1023955d011SMarcel Moolenaar 
1033955d011SMarcel Moolenaar #if !defined(HAVE_STRSEP)
104e2eeea75SSimon J. Gerraty # define strsep(s, d) stresep((s), (d), '\0')
1053955d011SMarcel Moolenaar #endif
10612904384SSimon J. Gerraty #if !defined(HAVE_STRESEP)
10712904384SSimon J. Gerraty char * stresep(char **, const char *, int);
10812904384SSimon J. Gerraty #endif
1093955d011SMarcel Moolenaar 
1103955d011SMarcel Moolenaar /*
1113955d011SMarcel Moolenaar  * Filemon is a kernel module which snoops certain syscalls.
1123955d011SMarcel Moolenaar  *
1133955d011SMarcel Moolenaar  * C chdir
1143955d011SMarcel Moolenaar  * E exec
1153955d011SMarcel Moolenaar  * F [v]fork
1163955d011SMarcel Moolenaar  * L [sym]link
1173955d011SMarcel Moolenaar  * M rename
1183955d011SMarcel Moolenaar  * R read
1193955d011SMarcel Moolenaar  * W write
1203955d011SMarcel Moolenaar  * S stat
1213955d011SMarcel Moolenaar  *
1223955d011SMarcel Moolenaar  * See meta_oodate below - we mainly care about 'E' and 'R'.
1233955d011SMarcel Moolenaar  *
1243955d011SMarcel Moolenaar  * We can still use meta mode without filemon, but
1253955d011SMarcel Moolenaar  * the benefits are more limited.
1263955d011SMarcel Moolenaar  */
1273955d011SMarcel Moolenaar #ifdef USE_FILEMON
1283955d011SMarcel Moolenaar 
1293955d011SMarcel Moolenaar /*
1303955d011SMarcel Moolenaar  * Open the filemon device.
1313955d011SMarcel Moolenaar  */
1323955d011SMarcel Moolenaar static void
meta_open_filemon(BuildMon * pbm)13349caa483SSimon J. Gerraty meta_open_filemon(BuildMon *pbm)
1343955d011SMarcel Moolenaar {
13549caa483SSimon J. Gerraty     int dupfd;
1363955d011SMarcel Moolenaar 
13749caa483SSimon J. Gerraty     pbm->mon_fd = -1;
13849caa483SSimon J. Gerraty     pbm->filemon = NULL;
13906b9b3e0SSimon J. Gerraty     if (!useFilemon || pbm->mfp == NULL)
1403955d011SMarcel Moolenaar 	return;
1413955d011SMarcel Moolenaar 
14249caa483SSimon J. Gerraty     pbm->filemon = filemon_open();
14349caa483SSimon J. Gerraty     if (pbm->filemon == NULL) {
144b0c40a00SSimon J. Gerraty 	useFilemon = false;
14549caa483SSimon J. Gerraty 	warn("Could not open filemon %s", filemon_path());
1463955d011SMarcel Moolenaar 	return;
1473955d011SMarcel Moolenaar     }
1483955d011SMarcel Moolenaar 
1493955d011SMarcel Moolenaar     /*
1503955d011SMarcel Moolenaar      * We use a file outside of '.'
1513955d011SMarcel Moolenaar      * to avoid a FreeBSD kernel bug where unlink invalidates
1523955d011SMarcel Moolenaar      * cwd causing getcwd to do a lot more work.
1533955d011SMarcel Moolenaar      * We only care about the descriptor.
1543955d011SMarcel Moolenaar      */
155dba7b0efSSimon J. Gerraty     if (!opts.compatMake)
156dba7b0efSSimon J. Gerraty 	pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
157dba7b0efSSimon J. Gerraty     else
158dba7b0efSSimon J. Gerraty 	pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
15949caa483SSimon J. Gerraty     if ((dupfd = dup(pbm->mon_fd)) == -1) {
16012904384SSimon J. Gerraty 	Punt("Could not dup filemon output: %s", strerror(errno));
16149caa483SSimon J. Gerraty     }
16249caa483SSimon J. Gerraty     (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
16349caa483SSimon J. Gerraty     if (filemon_setfd(pbm->filemon, dupfd) == -1) {
16412904384SSimon J. Gerraty 	Punt("Could not set filemon file descriptor: %s", strerror(errno));
1653955d011SMarcel Moolenaar     }
1663955d011SMarcel Moolenaar     /* we don't need these once we exec */
167be19d90bSSimon J. Gerraty     (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
1683955d011SMarcel Moolenaar }
1693955d011SMarcel Moolenaar 
1703955d011SMarcel Moolenaar /*
1713955d011SMarcel Moolenaar  * Read the build monitor output file and write records to the target's
1723955d011SMarcel Moolenaar  * metadata file.
1733955d011SMarcel Moolenaar  */
174e48f47ddSSimon J. Gerraty static int
filemon_read(FILE * mfp,int fd)1753955d011SMarcel Moolenaar filemon_read(FILE *mfp, int fd)
1763955d011SMarcel Moolenaar {
1773955d011SMarcel Moolenaar     char buf[BUFSIZ];
178e48f47ddSSimon J. Gerraty     int error;
1793955d011SMarcel Moolenaar 
1803955d011SMarcel Moolenaar     /* Check if we're not writing to a meta data file.*/
1813955d011SMarcel Moolenaar     if (mfp == NULL) {
1823955d011SMarcel Moolenaar 	if (fd >= 0)
1833955d011SMarcel Moolenaar 	    close(fd);			/* not interested */
184e48f47ddSSimon J. Gerraty 	return 0;
1853955d011SMarcel Moolenaar     }
1863955d011SMarcel Moolenaar     /* rewind */
1873841c287SSimon J. Gerraty     if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
1883841c287SSimon J. Gerraty 	error = errno;
1893841c287SSimon J. Gerraty 	warn("Could not rewind filemon");
1903841c287SSimon J. Gerraty 	fprintf(mfp, "\n");
1913841c287SSimon J. Gerraty     } else {
192956e45f6SSimon J. Gerraty 	ssize_t n;
193956e45f6SSimon J. Gerraty 
194e48f47ddSSimon J. Gerraty 	error = 0;
195bedf67aeSSimon J. Gerraty 	fprintf(mfp, "\n-- filemon acquired metadata --\n");
1963955d011SMarcel Moolenaar 
197e2eeea75SSimon J. Gerraty 	while ((n = read(fd, buf, sizeof buf)) > 0) {
198956e45f6SSimon J. Gerraty 	    if ((ssize_t)fwrite(buf, 1, (size_t)n, mfp) < n)
199e48f47ddSSimon J. Gerraty 		error = EIO;
2003955d011SMarcel Moolenaar 	}
2013841c287SSimon J. Gerraty     }
20212904384SSimon J. Gerraty     if (fflush(mfp) != 0)
20312904384SSimon J. Gerraty 	Punt("Cannot write filemon data to meta file: %s",
20412904384SSimon J. Gerraty 	    strerror(errno));
205e48f47ddSSimon J. Gerraty     if (close(fd) < 0)
206e48f47ddSSimon J. Gerraty 	error = errno;
207e48f47ddSSimon J. Gerraty     return error;
2083955d011SMarcel Moolenaar }
2093955d011SMarcel Moolenaar #endif
2103955d011SMarcel Moolenaar 
2113955d011SMarcel Moolenaar /*
2123955d011SMarcel Moolenaar  * when realpath() fails,
2133955d011SMarcel Moolenaar  * we use this, to clean up ./ and ../
2143955d011SMarcel Moolenaar  */
2153955d011SMarcel Moolenaar static void
eat_dots(char * buf)2169f45a3c8SSimon J. Gerraty eat_dots(char *buf)
2173955d011SMarcel Moolenaar {
2189f45a3c8SSimon J. Gerraty     char *p;
2193955d011SMarcel Moolenaar 
2209f45a3c8SSimon J. Gerraty     while ((p = strstr(buf, "/./")) != NULL)
2219f45a3c8SSimon J. Gerraty 	memmove(p, p + 2, strlen(p + 2) + 1);
2223955d011SMarcel Moolenaar 
2239f45a3c8SSimon J. Gerraty     while ((p = strstr(buf, "/../")) != NULL) {
2249f45a3c8SSimon J. Gerraty 	char *p2 = p + 3;
2259f45a3c8SSimon J. Gerraty 	if (p > buf) {
2263955d011SMarcel Moolenaar 	    do {
2279f45a3c8SSimon J. Gerraty 		p--;
2289f45a3c8SSimon J. Gerraty 	    } while (p > buf && *p != '/');
2293955d011SMarcel Moolenaar 	}
2309f45a3c8SSimon J. Gerraty 	if (*p == '/')
2319f45a3c8SSimon J. Gerraty 	    memmove(p, p2, strlen(p2) + 1);
2329f45a3c8SSimon J. Gerraty 	else
2333955d011SMarcel Moolenaar 	    return;		/* can't happen? */
2343955d011SMarcel Moolenaar     }
2353955d011SMarcel Moolenaar }
2363955d011SMarcel Moolenaar 
2373955d011SMarcel Moolenaar static char *
meta_name(char * mname,size_t mnamelen,const char * dname,const char * tname,const char * cwd)2381ce939a7SSimon J. Gerraty meta_name(char *mname, size_t mnamelen,
2393955d011SMarcel Moolenaar 	  const char *dname,
240b778b302SSimon J. Gerraty 	  const char *tname,
241b778b302SSimon J. Gerraty 	  const char *cwd)
2423955d011SMarcel Moolenaar {
2433955d011SMarcel Moolenaar     char buf[MAXPATHLEN];
24412904384SSimon J. Gerraty     char *rp, *cp;
24512904384SSimon J. Gerraty     const char *tname_base;
2463955d011SMarcel Moolenaar     char *tp;
247e22fef7dSSimon J. Gerraty     char *dtp;
248e22fef7dSSimon J. Gerraty     size_t ldname;
2493955d011SMarcel Moolenaar 
2503955d011SMarcel Moolenaar     /*
2513955d011SMarcel Moolenaar      * Weed out relative paths from the target file name.
2523955d011SMarcel Moolenaar      * We have to be careful though since if target is a
2533955d011SMarcel Moolenaar      * symlink, the result will be unstable.
2543955d011SMarcel Moolenaar      * So we use realpath() just to get the dirname, and leave the
2553955d011SMarcel Moolenaar      * basename as given to us.
2563955d011SMarcel Moolenaar      */
25712904384SSimon J. Gerraty     if ((tname_base = strrchr(tname, '/')) != NULL) {
25806b9b3e0SSimon J. Gerraty 	if (cached_realpath(tname, buf) != NULL) {
25906b9b3e0SSimon J. Gerraty 	    if ((rp = strrchr(buf, '/')) != NULL) {
2603955d011SMarcel Moolenaar 		rp++;
26112904384SSimon J. Gerraty 		tname_base++;
26212904384SSimon J. Gerraty 		if (strcmp(tname_base, rp) != 0)
26312904384SSimon J. Gerraty 		    strlcpy(rp, tname_base, sizeof buf - (size_t)(rp - buf));
2643955d011SMarcel Moolenaar 	    }
2653955d011SMarcel Moolenaar 	    tname = buf;
2663955d011SMarcel Moolenaar 	} else {
2673955d011SMarcel Moolenaar 	    /*
2683955d011SMarcel Moolenaar 	     * We likely have a directory which is about to be made.
2693955d011SMarcel Moolenaar 	     * We pretend realpath() succeeded, to have a chance
2703955d011SMarcel Moolenaar 	     * of generating the same meta file name that we will
2713955d011SMarcel Moolenaar 	     * next time through.
2723955d011SMarcel Moolenaar 	     */
2733955d011SMarcel Moolenaar 	    if (tname[0] == '/') {
274e2eeea75SSimon J. Gerraty 		strlcpy(buf, tname, sizeof buf);
2753955d011SMarcel Moolenaar 	    } else {
276e2eeea75SSimon J. Gerraty 		snprintf(buf, sizeof buf, "%s/%s", cwd, tname);
2773955d011SMarcel Moolenaar 	    }
2789f45a3c8SSimon J. Gerraty 	    eat_dots(buf);
2793955d011SMarcel Moolenaar 	    tname = buf;
2803955d011SMarcel Moolenaar 	}
2813955d011SMarcel Moolenaar     }
2823955d011SMarcel Moolenaar     /* on some systems dirname may modify its arg */
2833955d011SMarcel Moolenaar     tp = bmake_strdup(tname);
284e22fef7dSSimon J. Gerraty     dtp = dirname(tp);
2854fde40d9SSimon J. Gerraty     if (strcmp(dname, dtp) == 0) {
2864fde40d9SSimon J. Gerraty 	if (snprintf(mname, mnamelen, "%s.meta", tname) >= (int)mnamelen)
2874fde40d9SSimon J. Gerraty 	    mname[mnamelen - 1] = '\0';
2884fde40d9SSimon J. Gerraty     } else {
2894fde40d9SSimon J. Gerraty 	int x;
2904fde40d9SSimon J. Gerraty 
291e22fef7dSSimon J. Gerraty 	ldname = strlen(dname);
292e22fef7dSSimon J. Gerraty 	if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
2934fde40d9SSimon J. Gerraty 	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
294e22fef7dSSimon J. Gerraty 	else
2954fde40d9SSimon J. Gerraty 	    x = snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
2964fde40d9SSimon J. Gerraty 	if (x >= (int)mnamelen)
2974fde40d9SSimon J. Gerraty 	    mname[mnamelen - 1] = '\0';
2983955d011SMarcel Moolenaar 	/*
2993955d011SMarcel Moolenaar 	 * Replace path separators in the file name after the
3003955d011SMarcel Moolenaar 	 * current object directory path.
3013955d011SMarcel Moolenaar 	 */
3023955d011SMarcel Moolenaar 	cp = mname + strlen(dname) + 1;
3033955d011SMarcel Moolenaar 
3043955d011SMarcel Moolenaar 	while (*cp != '\0') {
3053955d011SMarcel Moolenaar 	    if (*cp == '/')
3063955d011SMarcel Moolenaar 		*cp = '_';
3073955d011SMarcel Moolenaar 	    cp++;
3083955d011SMarcel Moolenaar 	}
3093955d011SMarcel Moolenaar     }
3103955d011SMarcel Moolenaar     free(tp);
3113841c287SSimon J. Gerraty     return mname;
3123955d011SMarcel Moolenaar }
3133955d011SMarcel Moolenaar 
3143955d011SMarcel Moolenaar /*
3153955d011SMarcel Moolenaar  * Return true if running ${.MAKE}
3163955d011SMarcel Moolenaar  * Bypassed if target is flagged .MAKE
3173955d011SMarcel Moolenaar  */
318b0c40a00SSimon J. Gerraty static bool
is_submake(const char * cmd,GNode * gn)31906b9b3e0SSimon J. Gerraty is_submake(const char *cmd, GNode *gn)
3203955d011SMarcel Moolenaar {
3212c3632d1SSimon J. Gerraty     static const char *p_make = NULL;
322956e45f6SSimon J. Gerraty     static size_t p_len;
3233955d011SMarcel Moolenaar     char *mp = NULL;
3249f45a3c8SSimon J. Gerraty     const char *cp2;
325b0c40a00SSimon J. Gerraty     bool rc = false;
3263955d011SMarcel Moolenaar 
327e2eeea75SSimon J. Gerraty     if (p_make == NULL) {
328dba7b0efSSimon J. Gerraty 	p_make = Var_Value(gn, ".MAKE").str;
3293955d011SMarcel Moolenaar 	p_len = strlen(p_make);
3303955d011SMarcel Moolenaar     }
3319f45a3c8SSimon J. Gerraty     if (strchr(cmd, '$') != NULL) {
3328d5c8e21SSimon J. Gerraty 	mp = Var_Subst(cmd, gn, VARE_EVAL);
333956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
3343955d011SMarcel Moolenaar 	cmd = mp;
3353955d011SMarcel Moolenaar     }
3363955d011SMarcel Moolenaar     cp2 = strstr(cmd, p_make);
337e2eeea75SSimon J. Gerraty     if (cp2 != NULL) {
3383955d011SMarcel Moolenaar 	switch (cp2[p_len]) {
3393955d011SMarcel Moolenaar 	case '\0':
3403955d011SMarcel Moolenaar 	case ' ':
3413955d011SMarcel Moolenaar 	case '\t':
3423955d011SMarcel Moolenaar 	case '\n':
343b0c40a00SSimon J. Gerraty 	    rc = true;
3443955d011SMarcel Moolenaar 	    break;
3453955d011SMarcel Moolenaar 	}
34606b9b3e0SSimon J. Gerraty 	if (cp2 > cmd && rc) {
3473955d011SMarcel Moolenaar 	    switch (cp2[-1]) {
3483955d011SMarcel Moolenaar 	    case ' ':
3493955d011SMarcel Moolenaar 	    case '\t':
3503955d011SMarcel Moolenaar 	    case '\n':
3513955d011SMarcel Moolenaar 		break;
3523955d011SMarcel Moolenaar 	    default:
353b0c40a00SSimon J. Gerraty 		rc = false;		/* no match */
3543955d011SMarcel Moolenaar 		break;
3553955d011SMarcel Moolenaar 	    }
3563955d011SMarcel Moolenaar 	}
3573955d011SMarcel Moolenaar     }
3583955d011SMarcel Moolenaar     free(mp);
3593841c287SSimon J. Gerraty     return rc;
3603955d011SMarcel Moolenaar }
3613955d011SMarcel Moolenaar 
362b0c40a00SSimon J. Gerraty static bool
any_is_submake(GNode * gn)36306b9b3e0SSimon J. Gerraty any_is_submake(GNode *gn)
36406b9b3e0SSimon J. Gerraty {
36506b9b3e0SSimon J. Gerraty     StringListNode *ln;
36606b9b3e0SSimon J. Gerraty 
36706b9b3e0SSimon J. Gerraty     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
36806b9b3e0SSimon J. Gerraty 	if (is_submake(ln->datum, gn))
369b0c40a00SSimon J. Gerraty 	    return true;
370b0c40a00SSimon J. Gerraty     return false;
37106b9b3e0SSimon J. Gerraty }
3723955d011SMarcel Moolenaar 
373956e45f6SSimon J. Gerraty static void
printCMD(const char * ucmd,FILE * fp,GNode * gn)374b0c40a00SSimon J. Gerraty printCMD(const char *ucmd, FILE *fp, GNode *gn)
3753955d011SMarcel Moolenaar {
376b0c40a00SSimon J. Gerraty     FStr xcmd = FStr_InitRefer(ucmd);
3773955d011SMarcel Moolenaar 
3788d5c8e21SSimon J. Gerraty     Var_Expand(&xcmd, gn, VARE_EVAL);
379b0c40a00SSimon J. Gerraty     fprintf(fp, "CMD %s\n", xcmd.str);
380b0c40a00SSimon J. Gerraty     FStr_Done(&xcmd);
381956e45f6SSimon J. Gerraty }
382956e45f6SSimon J. Gerraty 
383956e45f6SSimon J. Gerraty static void
printCMDs(GNode * gn,FILE * fp)38406b9b3e0SSimon J. Gerraty printCMDs(GNode *gn, FILE *fp)
385956e45f6SSimon J. Gerraty {
38606b9b3e0SSimon J. Gerraty     StringListNode *ln;
387956e45f6SSimon J. Gerraty 
38806b9b3e0SSimon J. Gerraty     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
38906b9b3e0SSimon J. Gerraty 	printCMD(ln->datum, fp, gn);
3903955d011SMarcel Moolenaar }
3913955d011SMarcel Moolenaar 
3923955d011SMarcel Moolenaar /*
3933955d011SMarcel Moolenaar  * Certain node types never get a .meta file
3943955d011SMarcel Moolenaar  */
3952f2a5ecdSSimon J. Gerraty #define SKIP_META_TYPE(flag, str) do { \
3962f2a5ecdSSimon J. Gerraty     if ((gn->type & (flag))) { \
3972f2a5ecdSSimon J. Gerraty 	if (verbose) \
3982f2a5ecdSSimon J. Gerraty 	    debug_printf("Skipping meta for %s: .%s\n", gn->name, str); \
399b0c40a00SSimon J. Gerraty 	return false; \
4003955d011SMarcel Moolenaar     } \
40112904384SSimon J. Gerraty } while (false)
4023955d011SMarcel Moolenaar 
403b778b302SSimon J. Gerraty 
404b778b302SSimon J. Gerraty /*
405b778b302SSimon J. Gerraty  * Do we need/want a .meta file ?
406b778b302SSimon J. Gerraty  */
407b0c40a00SSimon J. Gerraty static bool
meta_needed(GNode * gn,const char * dname,char * objdir_realpath,bool verbose)4081ce939a7SSimon J. Gerraty meta_needed(GNode *gn, const char *dname,
409b0c40a00SSimon J. Gerraty 	    char *objdir_realpath, bool verbose)
410b778b302SSimon J. Gerraty {
411e2eeea75SSimon J. Gerraty     struct cached_stat cst;
412b778b302SSimon J. Gerraty 
413b778b302SSimon J. Gerraty     if (verbose)
414b778b302SSimon J. Gerraty 	verbose = DEBUG(META);
415b778b302SSimon J. Gerraty 
416b778b302SSimon J. Gerraty     /* This may be a phony node which we don't want meta data for... */
417b778b302SSimon J. Gerraty     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
418b778b302SSimon J. Gerraty     /* Or it may be explicitly flagged as .NOMETA */
4192f2a5ecdSSimon J. Gerraty     SKIP_META_TYPE(OP_NOMETA, "NOMETA");
420b778b302SSimon J. Gerraty     /* Unless it is explicitly flagged as .META */
421b778b302SSimon J. Gerraty     if (!(gn->type & OP_META)) {
4222f2a5ecdSSimon J. Gerraty 	SKIP_META_TYPE(OP_PHONY, "PHONY");
4232f2a5ecdSSimon J. Gerraty 	SKIP_META_TYPE(OP_SPECIAL, "SPECIAL");
4242f2a5ecdSSimon J. Gerraty 	SKIP_META_TYPE(OP_MAKE, "MAKE");
425b778b302SSimon J. Gerraty     }
426b778b302SSimon J. Gerraty 
427b778b302SSimon J. Gerraty     /* Check if there are no commands to execute. */
42806b9b3e0SSimon J. Gerraty     if (Lst_IsEmpty(&gn->commands)) {
429b778b302SSimon J. Gerraty 	if (verbose)
430956e45f6SSimon J. Gerraty 	    debug_printf("Skipping meta for %s: no commands\n", gn->name);
431b0c40a00SSimon J. Gerraty 	return false;
432b778b302SSimon J. Gerraty     }
433b778b302SSimon J. Gerraty     if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
434b778b302SSimon J. Gerraty 	/* OP_SUBMAKE is a bit too aggressive */
43506b9b3e0SSimon J. Gerraty 	if (any_is_submake(gn)) {
436956e45f6SSimon J. Gerraty 	    DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
437b0c40a00SSimon J. Gerraty 	    return false;
438b778b302SSimon J. Gerraty 	}
439b778b302SSimon J. Gerraty     }
440b778b302SSimon J. Gerraty 
441b778b302SSimon J. Gerraty     /* The object directory may not exist. Check it.. */
442e2eeea75SSimon J. Gerraty     if (cached_stat(dname, &cst) != 0) {
443b778b302SSimon J. Gerraty 	if (verbose)
444956e45f6SSimon J. Gerraty 	    debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
445b0c40a00SSimon J. Gerraty 	return false;
446b778b302SSimon J. Gerraty     }
447b778b302SSimon J. Gerraty 
448b778b302SSimon J. Gerraty     /* make sure these are canonical */
44906b9b3e0SSimon J. Gerraty     if (cached_realpath(dname, objdir_realpath) != NULL)
45006b9b3e0SSimon J. Gerraty 	dname = objdir_realpath;
451b778b302SSimon J. Gerraty 
452b778b302SSimon J. Gerraty     /* If we aren't in the object directory, don't create a meta file. */
453b778b302SSimon J. Gerraty     if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
454b778b302SSimon J. Gerraty 	if (verbose)
455956e45f6SSimon J. Gerraty 	    debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
456b778b302SSimon J. Gerraty 			 gn->name);
457b0c40a00SSimon J. Gerraty 	return false;
458b778b302SSimon J. Gerraty     }
459b0c40a00SSimon J. Gerraty     return true;
460b778b302SSimon J. Gerraty }
461b778b302SSimon J. Gerraty 
462b778b302SSimon J. Gerraty 
4633955d011SMarcel Moolenaar static FILE *
meta_create(BuildMon * pbm,GNode * gn)4643955d011SMarcel Moolenaar meta_create(BuildMon *pbm, GNode *gn)
4653955d011SMarcel Moolenaar {
46606b9b3e0SSimon J. Gerraty     FILE *fp;
4673955d011SMarcel Moolenaar     char buf[MAXPATHLEN];
46806b9b3e0SSimon J. Gerraty     char objdir_realpath[MAXPATHLEN];
4693955d011SMarcel Moolenaar     char **ptr;
47006b9b3e0SSimon J. Gerraty     FStr dname;
4713955d011SMarcel Moolenaar     const char *tname;
4723955d011SMarcel Moolenaar     char *fname;
4733955d011SMarcel Moolenaar     const char *cp;
4743955d011SMarcel Moolenaar 
47506b9b3e0SSimon J. Gerraty     fp = NULL;
4763955d011SMarcel Moolenaar 
477dba7b0efSSimon J. Gerraty     dname = Var_Value(gn, ".OBJDIR");
478956e45f6SSimon J. Gerraty     tname = GNode_VarTarget(gn);
4793955d011SMarcel Moolenaar 
48006b9b3e0SSimon J. Gerraty     /* if this succeeds objdir_realpath is realpath of dname */
481b0c40a00SSimon J. Gerraty     if (!meta_needed(gn, dname.str, objdir_realpath, true))
4823955d011SMarcel Moolenaar 	goto out;
48306b9b3e0SSimon J. Gerraty     dname.str = objdir_realpath;
4843955d011SMarcel Moolenaar 
4853955d011SMarcel Moolenaar     if (metaVerbose) {
4863955d011SMarcel Moolenaar 	/* Describe the target we are building */
4878d5c8e21SSimon J. Gerraty 	char *mp = Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_EVAL);
488956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
48906b9b3e0SSimon J. Gerraty 	if (mp[0] != '\0')
4903955d011SMarcel Moolenaar 	    fprintf(stdout, "%s\n", mp);
4913955d011SMarcel Moolenaar 	free(mp);
4923955d011SMarcel Moolenaar     }
4933955d011SMarcel Moolenaar     /* Get the basename of the target */
49406b9b3e0SSimon J. Gerraty     cp = str_basename(tname);
4953955d011SMarcel Moolenaar 
4963955d011SMarcel Moolenaar     fflush(stdout);
4973955d011SMarcel Moolenaar 
4983955d011SMarcel Moolenaar     if (!writeMeta)
4993955d011SMarcel Moolenaar 	/* Don't create meta data. */
5003955d011SMarcel Moolenaar 	goto out;
5013955d011SMarcel Moolenaar 
502e2eeea75SSimon J. Gerraty     fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
50306b9b3e0SSimon J. Gerraty 		      dname.str, tname, objdir_realpath);
5043955d011SMarcel Moolenaar 
5053955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
506956e45f6SSimon J. Gerraty     DEBUG1(META, "meta_create: %s\n", fname);
5073955d011SMarcel Moolenaar #endif
5083955d011SMarcel Moolenaar 
50906b9b3e0SSimon J. Gerraty     if ((fp = fopen(fname, "w")) == NULL)
51012904384SSimon J. Gerraty 	Punt("Could not open meta file '%s': %s", fname, strerror(errno));
5113955d011SMarcel Moolenaar 
51206b9b3e0SSimon J. Gerraty     fprintf(fp, "# Meta data file %s\n", fname);
5133955d011SMarcel Moolenaar 
51406b9b3e0SSimon J. Gerraty     printCMDs(gn, fp);
5153955d011SMarcel Moolenaar 
51606b9b3e0SSimon J. Gerraty     fprintf(fp, "CWD %s\n", getcwd(buf, sizeof buf));
51706b9b3e0SSimon J. Gerraty     fprintf(fp, "TARGET %s\n", tname);
518956e45f6SSimon J. Gerraty     cp = GNode_VarOodate(gn);
51906b9b3e0SSimon J. Gerraty     if (cp != NULL && *cp != '\0') {
52006b9b3e0SSimon J. Gerraty 	fprintf(fp, "OODATE %s\n", cp);
52149caa483SSimon J. Gerraty     }
5223955d011SMarcel Moolenaar     if (metaEnv) {
5233955d011SMarcel Moolenaar 	for (ptr = environ; *ptr != NULL; ptr++)
52406b9b3e0SSimon J. Gerraty 	    fprintf(fp, "ENV %s\n", *ptr);
5253955d011SMarcel Moolenaar     }
5263955d011SMarcel Moolenaar 
52706b9b3e0SSimon J. Gerraty     fprintf(fp, "-- command output --\n");
52812904384SSimon J. Gerraty     if (fflush(fp) != 0)
52912904384SSimon J. Gerraty 	Punt("Cannot write expanded command to meta file: %s",
53012904384SSimon J. Gerraty 	    strerror(errno));
5313955d011SMarcel Moolenaar 
532dba7b0efSSimon J. Gerraty     Global_Append(".MAKE.META.FILES", fname);
533dba7b0efSSimon J. Gerraty     Global_Append(".MAKE.META.CREATED", fname);
5343955d011SMarcel Moolenaar 
5353955d011SMarcel Moolenaar     gn->type |= OP_META;		/* in case anyone wants to know */
5363955d011SMarcel Moolenaar     if (metaSilent) {
5373955d011SMarcel Moolenaar 	    gn->type |= OP_SILENT;
5383955d011SMarcel Moolenaar     }
5393955d011SMarcel Moolenaar  out:
54006b9b3e0SSimon J. Gerraty     FStr_Done(&dname);
5413955d011SMarcel Moolenaar 
54206b9b3e0SSimon J. Gerraty     return fp;
5433955d011SMarcel Moolenaar }
5443955d011SMarcel Moolenaar 
545b0c40a00SSimon J. Gerraty static bool
boolValue(const char * s)54612904384SSimon J. Gerraty boolValue(const char *s)
5473955d011SMarcel Moolenaar {
5483955d011SMarcel Moolenaar     switch (*s) {
5493955d011SMarcel Moolenaar     case '0':
5503955d011SMarcel Moolenaar     case 'N':
5513955d011SMarcel Moolenaar     case 'n':
5523955d011SMarcel Moolenaar     case 'F':
5533955d011SMarcel Moolenaar     case 'f':
554b0c40a00SSimon J. Gerraty 	return false;
5553955d011SMarcel Moolenaar     }
556b0c40a00SSimon J. Gerraty     return true;
5573955d011SMarcel Moolenaar }
5583955d011SMarcel Moolenaar 
5591748de26SSimon J. Gerraty /*
5601748de26SSimon J. Gerraty  * Initialization we need before reading makefiles.
5611748de26SSimon J. Gerraty  */
5623955d011SMarcel Moolenaar void
meta_init(void)56352d86256SSimon J. Gerraty meta_init(void)
5641748de26SSimon J. Gerraty {
5651748de26SSimon J. Gerraty #ifdef USE_FILEMON
5661748de26SSimon J. Gerraty 	/* this allows makefiles to test if we have filemon support */
567dba7b0efSSimon J. Gerraty 	Global_Set(".MAKE.PATH_FILEMON", filemon_path());
5681748de26SSimon J. Gerraty #endif
5691748de26SSimon J. Gerraty }
5701748de26SSimon J. Gerraty 
5711748de26SSimon J. Gerraty 
572b778b302SSimon J. Gerraty #define get_mode_bf(bf, token) \
57306b9b3e0SSimon J. Gerraty     if ((cp = strstr(make_mode, token)) != NULL) \
574e2eeea75SSimon J. Gerraty 	bf = boolValue(cp + sizeof (token) - 1)
575b778b302SSimon J. Gerraty 
5761748de26SSimon J. Gerraty /*
5771748de26SSimon J. Gerraty  * Initialization we need after reading makefiles.
5781748de26SSimon J. Gerraty  */
5791748de26SSimon J. Gerraty void
meta_mode_init(const char * make_mode)5801748de26SSimon J. Gerraty meta_mode_init(const char *make_mode)
5813955d011SMarcel Moolenaar {
582b0c40a00SSimon J. Gerraty     static bool once = false;
58312904384SSimon J. Gerraty     const char *cp;
5843955d011SMarcel Moolenaar 
585b0c40a00SSimon J. Gerraty     useMeta = true;
586b0c40a00SSimon J. Gerraty     useFilemon = true;
587b0c40a00SSimon J. Gerraty     writeMeta = true;
5883955d011SMarcel Moolenaar 
58906b9b3e0SSimon J. Gerraty     if (make_mode != NULL) {
59006b9b3e0SSimon J. Gerraty 	if (strstr(make_mode, "env") != NULL)
591b0c40a00SSimon J. Gerraty 	    metaEnv = true;
59206b9b3e0SSimon J. Gerraty 	if (strstr(make_mode, "verb") != NULL)
593b0c40a00SSimon J. Gerraty 	    metaVerbose = true;
59406b9b3e0SSimon J. Gerraty 	if (strstr(make_mode, "read") != NULL)
595b0c40a00SSimon J. Gerraty 	    writeMeta = false;
59606b9b3e0SSimon J. Gerraty 	if (strstr(make_mode, "nofilemon") != NULL)
597b0c40a00SSimon J. Gerraty 	    useFilemon = false;
59806b9b3e0SSimon J. Gerraty 	if (strstr(make_mode, "ignore-cmd") != NULL)
599b0c40a00SSimon J. Gerraty 	    metaIgnoreCMDs = true;
600b778b302SSimon J. Gerraty 	if (useFilemon)
601b778b302SSimon J. Gerraty 	    get_mode_bf(filemonMissing, "missing-filemon=");
602b778b302SSimon J. Gerraty 	get_mode_bf(metaCurdirOk, "curdirok=");
603b778b302SSimon J. Gerraty 	get_mode_bf(metaMissing, "missing-meta=");
604b778b302SSimon J. Gerraty 	get_mode_bf(metaSilent, "silent=");
6053955d011SMarcel Moolenaar     }
606dba7b0efSSimon J. Gerraty     if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
6073955d011SMarcel Moolenaar 	/*
6083955d011SMarcel Moolenaar 	 * The default value for MAKE_META_PREFIX
6093955d011SMarcel Moolenaar 	 * prints the absolute path of the target.
6103955d011SMarcel Moolenaar 	 * This works be cause :H will generate '.' if there is no /
6113955d011SMarcel Moolenaar 	 * and :tA will resolve that to cwd.
6123955d011SMarcel Moolenaar 	 */
613dba7b0efSSimon J. Gerraty 	Global_Set(MAKE_META_PREFIX,
614dba7b0efSSimon J. Gerraty 	    "Building ${.TARGET:H:tA}/${.TARGET:T}");
6153955d011SMarcel Moolenaar     }
6163955d011SMarcel Moolenaar     if (once)
6173955d011SMarcel Moolenaar 	return;
618b0c40a00SSimon J. Gerraty     once = true;
619e2eeea75SSimon J. Gerraty     memset(&Mybm, 0, sizeof Mybm);
6203955d011SMarcel Moolenaar     /*
6213955d011SMarcel Moolenaar      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
6223955d011SMarcel Moolenaar      */
6238c973ee2SSimon J. Gerraty     metaBailiwickStr = Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
6248d5c8e21SSimon J. Gerraty 				 SCOPE_GLOBAL, VARE_EVAL);
625956e45f6SSimon J. Gerraty     /* TODO: handle errors */
626d5e0a182SSimon J. Gerraty     AppendWords(&metaBailiwick, metaBailiwickStr);
62751ee2c1cSSimon J. Gerraty     /*
62851ee2c1cSSimon J. Gerraty      * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
62951ee2c1cSSimon J. Gerraty      */
630dba7b0efSSimon J. Gerraty     Global_Append(MAKE_META_IGNORE_PATHS,
631dba7b0efSSimon J. Gerraty 	       "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}");
6328c973ee2SSimon J. Gerraty     metaIgnorePathsStr = Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
6338d5c8e21SSimon J. Gerraty 				   SCOPE_GLOBAL, VARE_EVAL);
634956e45f6SSimon J. Gerraty     /* TODO: handle errors */
635d5e0a182SSimon J. Gerraty     AppendWords(&metaIgnorePaths, metaIgnorePathsStr);
636e48f47ddSSimon J. Gerraty 
637e48f47ddSSimon J. Gerraty     /*
638e48f47ddSSimon J. Gerraty      * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
639e48f47ddSSimon J. Gerraty      */
6409f45a3c8SSimon J. Gerraty     metaIgnorePatterns = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
6419f45a3c8SSimon J. Gerraty     metaIgnoreFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
6429f45a3c8SSimon J. Gerraty     metaCmpFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_CMP_FILTER);
6433955d011SMarcel Moolenaar }
6443955d011SMarcel Moolenaar 
6458c973ee2SSimon J. Gerraty MAKE_INLINE BuildMon *
BM(Job * job)6468c973ee2SSimon J. Gerraty BM(Job *job)
6478c973ee2SSimon J. Gerraty {
6488c973ee2SSimon J. Gerraty 
6498c973ee2SSimon J. Gerraty 	return ((job != NULL) ? &job->bm : &Mybm);
6508c973ee2SSimon J. Gerraty }
6518c973ee2SSimon J. Gerraty 
6523955d011SMarcel Moolenaar /*
6533955d011SMarcel Moolenaar  * In each case below we allow for job==NULL
6543955d011SMarcel Moolenaar  */
6553955d011SMarcel Moolenaar void
meta_job_start(Job * job,GNode * gn)6563955d011SMarcel Moolenaar meta_job_start(Job *job, GNode *gn)
6573955d011SMarcel Moolenaar {
6583955d011SMarcel Moolenaar     BuildMon *pbm;
6593955d011SMarcel Moolenaar 
6608c973ee2SSimon J. Gerraty     pbm = BM(job);
6613955d011SMarcel Moolenaar     pbm->mfp = meta_create(pbm, gn);
6623955d011SMarcel Moolenaar #ifdef USE_FILEMON_ONCE
6633955d011SMarcel Moolenaar     /* compat mode we open the filemon dev once per command */
6643955d011SMarcel Moolenaar     if (job == NULL)
6653955d011SMarcel Moolenaar 	return;
6663955d011SMarcel Moolenaar #endif
6673955d011SMarcel Moolenaar #ifdef USE_FILEMON
6683955d011SMarcel Moolenaar     if (pbm->mfp != NULL && useFilemon) {
66949caa483SSimon J. Gerraty 	meta_open_filemon(pbm);
6703955d011SMarcel Moolenaar     } else {
67149caa483SSimon J. Gerraty 	pbm->mon_fd = -1;
67249caa483SSimon J. Gerraty 	pbm->filemon = NULL;
6733955d011SMarcel Moolenaar     }
6743955d011SMarcel Moolenaar #endif
6753955d011SMarcel Moolenaar }
6763955d011SMarcel Moolenaar 
6773955d011SMarcel Moolenaar /*
6783955d011SMarcel Moolenaar  * The child calls this before doing anything.
6793955d011SMarcel Moolenaar  * It does not disturb our state.
6803955d011SMarcel Moolenaar  */
6813955d011SMarcel Moolenaar void
meta_job_child(Job * job MAKE_ATTR_UNUSED)6821d3f2ddcSSimon J. Gerraty meta_job_child(Job *job MAKE_ATTR_UNUSED)
6833955d011SMarcel Moolenaar {
6843955d011SMarcel Moolenaar #ifdef USE_FILEMON
6853955d011SMarcel Moolenaar     BuildMon *pbm;
6863955d011SMarcel Moolenaar 
6878c973ee2SSimon J. Gerraty     pbm = BM(job);
688db29cad8SSimon J. Gerraty     if (pbm->mfp != NULL) {
689db29cad8SSimon J. Gerraty 	close(fileno(pbm->mfp));
69006b9b3e0SSimon J. Gerraty 	if (useFilemon && pbm->filemon != NULL) {
691db29cad8SSimon J. Gerraty 	    pid_t pid;
692db29cad8SSimon J. Gerraty 
6933955d011SMarcel Moolenaar 	    pid = getpid();
69449caa483SSimon J. Gerraty 	    if (filemon_setpid_child(pbm->filemon, pid) == -1) {
69512904384SSimon J. Gerraty 		Punt("Could not set filemon pid: %s", strerror(errno));
6963955d011SMarcel Moolenaar 	    }
6973955d011SMarcel Moolenaar 	}
698db29cad8SSimon J. Gerraty     }
6993955d011SMarcel Moolenaar #endif
7003955d011SMarcel Moolenaar }
7013955d011SMarcel Moolenaar 
7023955d011SMarcel Moolenaar void
meta_job_parent(Job * job MAKE_ATTR_UNUSED,pid_t pid MAKE_ATTR_UNUSED)7031d3f2ddcSSimon J. Gerraty meta_job_parent(Job *job MAKE_ATTR_UNUSED, pid_t pid MAKE_ATTR_UNUSED)
70449caa483SSimon J. Gerraty {
70549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
70649caa483SSimon J. Gerraty     BuildMon *pbm;
70749caa483SSimon J. Gerraty 
7088c973ee2SSimon J. Gerraty     pbm = BM(job);
70906b9b3e0SSimon J. Gerraty     if (useFilemon && pbm->filemon != NULL) {
71049caa483SSimon J. Gerraty 	filemon_setpid_parent(pbm->filemon, pid);
71149caa483SSimon J. Gerraty     }
71249caa483SSimon J. Gerraty #endif
71349caa483SSimon J. Gerraty }
71449caa483SSimon J. Gerraty 
71549caa483SSimon J. Gerraty int
meta_job_fd(Job * job MAKE_ATTR_UNUSED)7161d3f2ddcSSimon J. Gerraty meta_job_fd(Job *job MAKE_ATTR_UNUSED)
71749caa483SSimon J. Gerraty {
71849caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
71949caa483SSimon J. Gerraty     BuildMon *pbm;
72049caa483SSimon J. Gerraty 
7218c973ee2SSimon J. Gerraty     pbm = BM(job);
72206b9b3e0SSimon J. Gerraty     if (useFilemon && pbm->filemon != NULL) {
72349caa483SSimon J. Gerraty 	return filemon_readfd(pbm->filemon);
72449caa483SSimon J. Gerraty     }
72549caa483SSimon J. Gerraty #endif
72649caa483SSimon J. Gerraty     return -1;
72749caa483SSimon J. Gerraty }
72849caa483SSimon J. Gerraty 
72949caa483SSimon J. Gerraty int
meta_job_event(Job * job MAKE_ATTR_UNUSED)7301d3f2ddcSSimon J. Gerraty meta_job_event(Job *job MAKE_ATTR_UNUSED)
73149caa483SSimon J. Gerraty {
73249caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
73349caa483SSimon J. Gerraty     BuildMon *pbm;
73449caa483SSimon J. Gerraty 
7358c973ee2SSimon J. Gerraty     pbm = BM(job);
73606b9b3e0SSimon J. Gerraty     if (useFilemon && pbm->filemon != NULL) {
73749caa483SSimon J. Gerraty 	return filemon_process(pbm->filemon);
73849caa483SSimon J. Gerraty     }
73949caa483SSimon J. Gerraty #endif
74049caa483SSimon J. Gerraty     return 0;
74149caa483SSimon J. Gerraty }
74249caa483SSimon J. Gerraty 
74349caa483SSimon J. Gerraty void
meta_job_error(Job * job,GNode * gn,bool ignerr,int status)744b0c40a00SSimon J. Gerraty meta_job_error(Job *job, GNode *gn, bool ignerr, int status)
7453955d011SMarcel Moolenaar {
7463955d011SMarcel Moolenaar     char cwd[MAXPATHLEN];
7473955d011SMarcel Moolenaar     BuildMon *pbm;
7483955d011SMarcel Moolenaar 
7498c973ee2SSimon J. Gerraty     pbm = BM(job);
7508c973ee2SSimon J. Gerraty     if (job != NULL && gn == NULL)
7513955d011SMarcel Moolenaar 	    gn = job->node;
7523955d011SMarcel Moolenaar     if (pbm->mfp != NULL) {
7538695518cSSimon J. Gerraty 	fprintf(pbm->mfp, "\n*** Error code %d%s\n",
75406b9b3e0SSimon J. Gerraty 		status, ignerr ? "(ignored)" : "");
7553955d011SMarcel Moolenaar     }
756dba7b0efSSimon J. Gerraty     if (gn != NULL)
757dba7b0efSSimon J. Gerraty 	Global_Set(".ERROR_TARGET", GNode_Path(gn));
7584fde40d9SSimon J. Gerraty     if (getcwd(cwd, sizeof cwd) == NULL)
7594fde40d9SSimon J. Gerraty 	Punt("Cannot get cwd: %s", strerror(errno));
7604fde40d9SSimon J. Gerraty 
761dba7b0efSSimon J. Gerraty     Global_Set(".ERROR_CWD", cwd);
762e2eeea75SSimon J. Gerraty     if (pbm->meta_fname[0] != '\0') {
763dba7b0efSSimon J. Gerraty 	Global_Set(".ERROR_META_FILE", pbm->meta_fname);
7643955d011SMarcel Moolenaar     }
7653955d011SMarcel Moolenaar     meta_job_finish(job);
7663955d011SMarcel Moolenaar }
7673955d011SMarcel Moolenaar 
7683955d011SMarcel Moolenaar void
meta_job_output(Job * job,char * cp,const char * nl)7693955d011SMarcel Moolenaar meta_job_output(Job *job, char *cp, const char *nl)
7703955d011SMarcel Moolenaar {
7713955d011SMarcel Moolenaar     BuildMon *pbm;
7723955d011SMarcel Moolenaar 
7738c973ee2SSimon J. Gerraty     pbm = BM(job);
7743955d011SMarcel Moolenaar     if (pbm->mfp != NULL) {
7753955d011SMarcel Moolenaar 	if (metaVerbose) {
7763955d011SMarcel Moolenaar 	    static char *meta_prefix = NULL;
777956e45f6SSimon J. Gerraty 	    static size_t meta_prefix_len;
7783955d011SMarcel Moolenaar 
779e2eeea75SSimon J. Gerraty 	    if (meta_prefix == NULL) {
7803955d011SMarcel Moolenaar 		char *cp2;
7813955d011SMarcel Moolenaar 
7828c973ee2SSimon J. Gerraty 		meta_prefix = Var_Subst("${" MAKE_META_PREFIX "}",
7838d5c8e21SSimon J. Gerraty 					SCOPE_GLOBAL, VARE_EVAL);
784956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
78506b9b3e0SSimon J. Gerraty 		if ((cp2 = strchr(meta_prefix, '$')) != NULL)
786956e45f6SSimon J. Gerraty 		    meta_prefix_len = (size_t)(cp2 - meta_prefix);
7873955d011SMarcel Moolenaar 		else
7883955d011SMarcel Moolenaar 		    meta_prefix_len = strlen(meta_prefix);
7893955d011SMarcel Moolenaar 	    }
7903955d011SMarcel Moolenaar 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
7913955d011SMarcel Moolenaar 		cp = strchr(cp + 1, '\n');
79206b9b3e0SSimon J. Gerraty 		if (cp == NULL)
7933955d011SMarcel Moolenaar 		    return;
79406b9b3e0SSimon J. Gerraty 		cp++;
7953955d011SMarcel Moolenaar 	    }
7963955d011SMarcel Moolenaar 	}
7973955d011SMarcel Moolenaar 	fprintf(pbm->mfp, "%s%s", cp, nl);
7983955d011SMarcel Moolenaar     }
7993955d011SMarcel Moolenaar }
8003955d011SMarcel Moolenaar 
801e48f47ddSSimon J. Gerraty int
meta_cmd_finish(void * pbmp)8023955d011SMarcel Moolenaar meta_cmd_finish(void *pbmp)
8033955d011SMarcel Moolenaar {
804e48f47ddSSimon J. Gerraty     int error = 0;
8053955d011SMarcel Moolenaar     BuildMon *pbm = pbmp;
80650d2e745SSimon J. Gerraty #ifdef USE_FILEMON
807e48f47ddSSimon J. Gerraty     int x;
80850d2e745SSimon J. Gerraty #endif
8093955d011SMarcel Moolenaar 
810e2eeea75SSimon J. Gerraty     if (pbm == NULL)
8113955d011SMarcel Moolenaar 	pbm = &Mybm;
8123955d011SMarcel Moolenaar 
8138695518cSSimon J. Gerraty #ifdef USE_FILEMON
81406b9b3e0SSimon J. Gerraty     if (pbm->filemon != NULL) {
81549caa483SSimon J. Gerraty 	while (filemon_process(pbm->filemon) > 0)
81649caa483SSimon J. Gerraty 	    continue;
81712904384SSimon J. Gerraty 	if (filemon_close(pbm->filemon) == -1) {
818e48f47ddSSimon J. Gerraty 	    error = errno;
81912904384SSimon J. Gerraty 	    Punt("filemon failed: %s", strerror(errno));
82012904384SSimon J. Gerraty 	}
821e48f47ddSSimon J. Gerraty 	x = filemon_read(pbm->mfp, pbm->mon_fd);
822e48f47ddSSimon J. Gerraty 	if (error == 0 && x != 0)
823e48f47ddSSimon J. Gerraty 	    error = x;
82449caa483SSimon J. Gerraty 	pbm->mon_fd = -1;
82549caa483SSimon J. Gerraty 	pbm->filemon = NULL;
826e2eeea75SSimon J. Gerraty 	return error;
827e2eeea75SSimon J. Gerraty     }
8283955d011SMarcel Moolenaar #endif
829e2eeea75SSimon J. Gerraty 
8308695518cSSimon J. Gerraty     fprintf(pbm->mfp, "\n");	/* ensure end with newline */
831e48f47ddSSimon J. Gerraty     return error;
8323955d011SMarcel Moolenaar }
8333955d011SMarcel Moolenaar 
834e48f47ddSSimon J. Gerraty int
meta_job_finish(Job * job)8353955d011SMarcel Moolenaar meta_job_finish(Job *job)
8363955d011SMarcel Moolenaar {
8373955d011SMarcel Moolenaar     BuildMon *pbm;
838e48f47ddSSimon J. Gerraty     int error = 0;
839e48f47ddSSimon J. Gerraty     int x;
8403955d011SMarcel Moolenaar 
8418c973ee2SSimon J. Gerraty     pbm = BM(job);
8423955d011SMarcel Moolenaar     if (pbm->mfp != NULL) {
843e48f47ddSSimon J. Gerraty 	error = meta_cmd_finish(pbm);
844e48f47ddSSimon J. Gerraty 	x = fclose(pbm->mfp);
845e48f47ddSSimon J. Gerraty 	if (error == 0 && x != 0)
846e48f47ddSSimon J. Gerraty 	    error = errno;
8473955d011SMarcel Moolenaar 	pbm->mfp = NULL;
8483955d011SMarcel Moolenaar 	pbm->meta_fname[0] = '\0';
8493955d011SMarcel Moolenaar     }
850e48f47ddSSimon J. Gerraty     return error;
8513955d011SMarcel Moolenaar }
8523955d011SMarcel Moolenaar 
853be19d90bSSimon J. Gerraty void
meta_finish(void)854be19d90bSSimon J. Gerraty meta_finish(void)
855be19d90bSSimon J. Gerraty {
85606b9b3e0SSimon J. Gerraty     Lst_Done(&metaBailiwick);
857be19d90bSSimon J. Gerraty     free(metaBailiwickStr);
85806b9b3e0SSimon J. Gerraty     Lst_Done(&metaIgnorePaths);
859be19d90bSSimon J. Gerraty     free(metaIgnorePathsStr);
860be19d90bSSimon J. Gerraty }
861be19d90bSSimon J. Gerraty 
8623955d011SMarcel Moolenaar /*
8633955d011SMarcel Moolenaar  * Fetch a full line from fp - growing bufp if needed
8643955d011SMarcel Moolenaar  * Return length in bufp.
8653955d011SMarcel Moolenaar  */
8663955d011SMarcel Moolenaar static int
fgetLine(char ** bufp,size_t * szp,int o,FILE * fp)8673955d011SMarcel Moolenaar fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
8683955d011SMarcel Moolenaar {
8693955d011SMarcel Moolenaar     char *buf = *bufp;
8703955d011SMarcel Moolenaar     size_t bufsz = *szp;
8713955d011SMarcel Moolenaar     struct stat fs;
8723955d011SMarcel Moolenaar     int x;
8733955d011SMarcel Moolenaar 
874956e45f6SSimon J. Gerraty     if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
8753955d011SMarcel Moolenaar     check_newline:
876956e45f6SSimon J. Gerraty 	x = o + (int)strlen(&buf[o]);
8773955d011SMarcel Moolenaar 	if (buf[x - 1] == '\n')
8783955d011SMarcel Moolenaar 	    return x;
8793955d011SMarcel Moolenaar 	/*
8803955d011SMarcel Moolenaar 	 * We need to grow the buffer.
8813955d011SMarcel Moolenaar 	 * The meta file can give us a clue.
8823955d011SMarcel Moolenaar 	 */
8833955d011SMarcel Moolenaar 	if (fstat(fileno(fp), &fs) == 0) {
8843955d011SMarcel Moolenaar 	    size_t newsz;
8853955d011SMarcel Moolenaar 	    char *p;
8863955d011SMarcel Moolenaar 
887956e45f6SSimon J. Gerraty 	    newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
8883955d011SMarcel Moolenaar 	    if (newsz <= bufsz)
889956e45f6SSimon J. Gerraty 		newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
8908695518cSSimon J. Gerraty 	    if (newsz <= bufsz)
8918695518cSSimon J. Gerraty 		return x;		/* truncated */
89206b9b3e0SSimon J. Gerraty 	    DEBUG2(META, "growing buffer %u -> %u\n",
89306b9b3e0SSimon J. Gerraty 		   (unsigned)bufsz, (unsigned)newsz);
8943955d011SMarcel Moolenaar 	    p = bmake_realloc(buf, newsz);
8953955d011SMarcel Moolenaar 	    *bufp = buf = p;
8963955d011SMarcel Moolenaar 	    *szp = bufsz = newsz;
8973955d011SMarcel Moolenaar 	    /* fetch the rest */
898e2eeea75SSimon J. Gerraty 	    if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
8993955d011SMarcel Moolenaar 		return x;		/* truncated! */
9003955d011SMarcel Moolenaar 	    goto check_newline;
9013955d011SMarcel Moolenaar 	}
9023955d011SMarcel Moolenaar     }
9033955d011SMarcel Moolenaar     return 0;
9043955d011SMarcel Moolenaar }
9053955d011SMarcel Moolenaar 
906b0c40a00SSimon J. Gerraty static bool
prefix_match(const char * prefix,const char * path)90706b9b3e0SSimon J. Gerraty prefix_match(const char *prefix, const char *path)
9083955d011SMarcel Moolenaar {
9093955d011SMarcel Moolenaar     size_t n = strlen(prefix);
9103955d011SMarcel Moolenaar 
9113841c287SSimon J. Gerraty     return strncmp(path, prefix, n) == 0;
9123955d011SMarcel Moolenaar }
9133955d011SMarcel Moolenaar 
914b0c40a00SSimon J. Gerraty static bool
has_any_prefix(const char * path,StringList * prefixes)91506b9b3e0SSimon J. Gerraty has_any_prefix(const char *path, StringList *prefixes)
91606b9b3e0SSimon J. Gerraty {
91706b9b3e0SSimon J. Gerraty     StringListNode *ln;
91806b9b3e0SSimon J. Gerraty 
91906b9b3e0SSimon J. Gerraty     for (ln = prefixes->first; ln != NULL; ln = ln->next)
92006b9b3e0SSimon J. Gerraty 	if (prefix_match(ln->datum, path))
921b0c40a00SSimon J. Gerraty 	    return true;
922b0c40a00SSimon J. Gerraty     return false;
92306b9b3e0SSimon J. Gerraty }
92406b9b3e0SSimon J. Gerraty 
9252c3632d1SSimon J. Gerraty /* See if the path equals prefix or starts with "prefix/". */
926b0c40a00SSimon J. Gerraty static bool
path_starts_with(const char * path,const char * prefix)927956e45f6SSimon J. Gerraty path_starts_with(const char *path, const char *prefix)
92895e3ed2cSSimon J. Gerraty {
92995e3ed2cSSimon J. Gerraty     size_t n = strlen(prefix);
93095e3ed2cSSimon J. Gerraty 
9312c3632d1SSimon J. Gerraty     if (strncmp(path, prefix, n) != 0)
932b0c40a00SSimon J. Gerraty 	return false;
9332c3632d1SSimon J. Gerraty     return path[n] == '\0' || path[n] == '/';
93495e3ed2cSSimon J. Gerraty }
93595e3ed2cSSimon J. Gerraty 
936b0c40a00SSimon J. Gerraty static bool
meta_ignore(GNode * gn,const char * p)93795e3ed2cSSimon J. Gerraty meta_ignore(GNode *gn, const char *p)
93895e3ed2cSSimon J. Gerraty {
93995e3ed2cSSimon J. Gerraty     char fname[MAXPATHLEN];
94095e3ed2cSSimon J. Gerraty 
94195e3ed2cSSimon J. Gerraty     if (p == NULL)
942b0c40a00SSimon J. Gerraty 	return true;
94395e3ed2cSSimon J. Gerraty 
94495e3ed2cSSimon J. Gerraty     if (*p == '/') {
94598875883SSimon J. Gerraty 	/* first try the raw path "as is" */
94698875883SSimon J. Gerraty 	if (has_any_prefix(p, &metaIgnorePaths)) {
94798875883SSimon J. Gerraty #ifdef DEBUG_META_MODE
94898875883SSimon J. Gerraty 	    DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
94998875883SSimon J. Gerraty #endif
95098875883SSimon J. Gerraty 	    return true;
95198875883SSimon J. Gerraty 	}
95295e3ed2cSSimon J. Gerraty 	cached_realpath(p, fname); /* clean it up */
95306b9b3e0SSimon J. Gerraty 	if (has_any_prefix(fname, &metaIgnorePaths)) {
95495e3ed2cSSimon J. Gerraty #ifdef DEBUG_META_MODE
955956e45f6SSimon J. Gerraty 	    DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
95695e3ed2cSSimon J. Gerraty #endif
957b0c40a00SSimon J. Gerraty 	    return true;
95895e3ed2cSSimon J. Gerraty 	}
95995e3ed2cSSimon J. Gerraty     }
96095e3ed2cSSimon J. Gerraty 
96195e3ed2cSSimon J. Gerraty     if (metaIgnorePatterns) {
9622c3632d1SSimon J. Gerraty 	const char *expr;
96395e3ed2cSSimon J. Gerraty 	char *pm;
96495e3ed2cSSimon J. Gerraty 
965dba7b0efSSimon J. Gerraty 	/*
966dba7b0efSSimon J. Gerraty 	 * XXX: This variable is set on a target GNode but is not one of
967dba7b0efSSimon J. Gerraty 	 * the usual local variables.  It should be deleted afterwards.
968dba7b0efSSimon J. Gerraty 	 * Ideally it would not be created in the first place, just like
969dba7b0efSSimon J. Gerraty 	 * in a .for loop.
970dba7b0efSSimon J. Gerraty 	 */
971dba7b0efSSimon J. Gerraty 	Var_Set(gn, ".p.", p);
9722c3632d1SSimon J. Gerraty 	expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
9738d5c8e21SSimon J. Gerraty 	pm = Var_Subst(expr, gn, VARE_EVAL);
974956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
97506b9b3e0SSimon J. Gerraty 	if (pm[0] != '\0') {
97695e3ed2cSSimon J. Gerraty #ifdef DEBUG_META_MODE
977956e45f6SSimon J. Gerraty 	    DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
97895e3ed2cSSimon J. Gerraty #endif
97995e3ed2cSSimon J. Gerraty 	    free(pm);
980b0c40a00SSimon J. Gerraty 	    return true;
98195e3ed2cSSimon J. Gerraty 	}
98295e3ed2cSSimon J. Gerraty 	free(pm);
98395e3ed2cSSimon J. Gerraty     }
98495e3ed2cSSimon J. Gerraty 
98595e3ed2cSSimon J. Gerraty     if (metaIgnoreFilter) {
98695e3ed2cSSimon J. Gerraty 	char *fm;
98795e3ed2cSSimon J. Gerraty 
98895e3ed2cSSimon J. Gerraty 	/* skip if filter result is empty */
989e2eeea75SSimon J. Gerraty 	snprintf(fname, sizeof fname,
99095e3ed2cSSimon J. Gerraty 		 "${%s:L:${%s:ts:}}",
99195e3ed2cSSimon J. Gerraty 		 p, MAKE_META_IGNORE_FILTER);
9928d5c8e21SSimon J. Gerraty 	fm = Var_Subst(fname, gn, VARE_EVAL);
993956e45f6SSimon J. Gerraty 	/* TODO: handle errors */
99495e3ed2cSSimon J. Gerraty 	if (*fm == '\0') {
99595e3ed2cSSimon J. Gerraty #ifdef DEBUG_META_MODE
996956e45f6SSimon J. Gerraty 	    DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
99795e3ed2cSSimon J. Gerraty #endif
99895e3ed2cSSimon J. Gerraty 	    free(fm);
999b0c40a00SSimon J. Gerraty 	    return true;
100095e3ed2cSSimon J. Gerraty 	}
100195e3ed2cSSimon J. Gerraty 	free(fm);
100295e3ed2cSSimon J. Gerraty     }
1003b0c40a00SSimon J. Gerraty     return false;
100495e3ed2cSSimon J. Gerraty }
100595e3ed2cSSimon J. Gerraty 
10063955d011SMarcel Moolenaar /*
10073955d011SMarcel Moolenaar  * When running with 'meta' functionality, a target can be out-of-date
1008db29cad8SSimon J. Gerraty  * if any of the references in its meta data file is more recent.
10093955d011SMarcel Moolenaar  * We have to track the latestdir on a per-process basis.
10103955d011SMarcel Moolenaar  */
1011db29cad8SSimon J. Gerraty #define LCWD_VNAME_FMT ".meta.%d.lcwd"
10123955d011SMarcel Moolenaar #define LDIR_VNAME_FMT ".meta.%d.ldir"
10133955d011SMarcel Moolenaar 
10143955d011SMarcel Moolenaar /*
10153955d011SMarcel Moolenaar  * It is possible that a .meta file is corrupted,
10163955d011SMarcel Moolenaar  * if we detect this we want to reproduce it.
1017b0c40a00SSimon J. Gerraty  * Setting oodate true will have that effect.
10183955d011SMarcel Moolenaar  */
101906b9b3e0SSimon J. Gerraty #define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
10209f45a3c8SSimon J. Gerraty     warnx("%s: %u: malformed", fname, lineno); \
1021b0c40a00SSimon J. Gerraty     oodate = true; \
10223955d011SMarcel Moolenaar     continue; \
10233955d011SMarcel Moolenaar     }
10243955d011SMarcel Moolenaar 
10250dede8b0SSimon J. Gerraty #define DEQUOTE(p) if (*p == '\'') {	\
10260dede8b0SSimon J. Gerraty     char *ep; \
10270dede8b0SSimon J. Gerraty     p++; \
102806b9b3e0SSimon J. Gerraty     if ((ep = strchr(p, '\'')) != NULL) \
10290dede8b0SSimon J. Gerraty 	*ep = '\0'; \
10300dede8b0SSimon J. Gerraty     }
10310dede8b0SSimon J. Gerraty 
1032956e45f6SSimon J. Gerraty static void
append_if_new(StringList * list,const char * str)1033956e45f6SSimon J. Gerraty append_if_new(StringList *list, const char *str)
1034956e45f6SSimon J. Gerraty {
1035956e45f6SSimon J. Gerraty     StringListNode *ln;
1036956e45f6SSimon J. Gerraty 
1037956e45f6SSimon J. Gerraty     for (ln = list->first; ln != NULL; ln = ln->next)
1038956e45f6SSimon J. Gerraty 	if (strcmp(ln->datum, str) == 0)
1039956e45f6SSimon J. Gerraty 	    return;
1040956e45f6SSimon J. Gerraty     Lst_Append(list, bmake_strdup(str));
1041956e45f6SSimon J. Gerraty }
1042956e45f6SSimon J. Gerraty 
1043a6f0e10bSSimon J. Gerraty /* A "reserved" variable to store the command to be filtered */
1044a6f0e10bSSimon J. Gerraty #define META_CMD_FILTER_VAR ".MAKE.cmd_filtered"
1045a6f0e10bSSimon J. Gerraty 
10469f45a3c8SSimon J. Gerraty static char *
meta_filter_cmd(GNode * gn,char * s)1047a6f0e10bSSimon J. Gerraty meta_filter_cmd(GNode *gn, char *s)
10489f45a3c8SSimon J. Gerraty {
1049a6f0e10bSSimon J. Gerraty     Var_Set(gn, META_CMD_FILTER_VAR, s);
10508c973ee2SSimon J. Gerraty     s = Var_Subst(
10518c973ee2SSimon J. Gerraty 	"${" META_CMD_FILTER_VAR ":${" MAKE_META_CMP_FILTER ":ts:}}",
10528d5c8e21SSimon J. Gerraty 	gn, VARE_EVAL);
10539f45a3c8SSimon J. Gerraty     return s;
10549f45a3c8SSimon J. Gerraty }
10559f45a3c8SSimon J. Gerraty 
10569f45a3c8SSimon J. Gerraty static int
meta_cmd_cmp(GNode * gn,char * a,char * b,bool filter)10579f45a3c8SSimon J. Gerraty meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
10589f45a3c8SSimon J. Gerraty {
10599f45a3c8SSimon J. Gerraty     int rc;
10609f45a3c8SSimon J. Gerraty 
10619f45a3c8SSimon J. Gerraty     rc = strcmp(a, b);
10629f45a3c8SSimon J. Gerraty     if (rc == 0 || !filter)
10639f45a3c8SSimon J. Gerraty 	return rc;
1064a6f0e10bSSimon J. Gerraty     a = meta_filter_cmd(gn, a);
1065a6f0e10bSSimon J. Gerraty     b = meta_filter_cmd(gn, b);
10669f45a3c8SSimon J. Gerraty     rc = strcmp(a, b);
10679f45a3c8SSimon J. Gerraty     free(a);
10689f45a3c8SSimon J. Gerraty     free(b);
1069a6f0e10bSSimon J. Gerraty     Var_Delete(gn, META_CMD_FILTER_VAR);
10709f45a3c8SSimon J. Gerraty     return rc;
10719f45a3c8SSimon J. Gerraty }
10729f45a3c8SSimon J. Gerraty 
1073b0c40a00SSimon J. Gerraty bool
meta_oodate(GNode * gn,bool oodate)1074b0c40a00SSimon J. Gerraty meta_oodate(GNode *gn, bool oodate)
10753955d011SMarcel Moolenaar {
10763955d011SMarcel Moolenaar     static char *tmpdir = NULL;
10773955d011SMarcel Moolenaar     static char cwd[MAXPATHLEN];
1078db29cad8SSimon J. Gerraty     char lcwd_vname[64];
10793955d011SMarcel Moolenaar     char ldir_vname[64];
1080db29cad8SSimon J. Gerraty     char lcwd[MAXPATHLEN];
10813955d011SMarcel Moolenaar     char latestdir[MAXPATHLEN];
10823955d011SMarcel Moolenaar     char fname[MAXPATHLEN];
10833955d011SMarcel Moolenaar     char fname1[MAXPATHLEN];
10843955d011SMarcel Moolenaar     char fname2[MAXPATHLEN];
1085db29cad8SSimon J. Gerraty     char fname3[MAXPATHLEN];
108606b9b3e0SSimon J. Gerraty     FStr dname;
1087b778b302SSimon J. Gerraty     const char *tname;
10883955d011SMarcel Moolenaar     char *p;
10890dede8b0SSimon J. Gerraty     char *link_src;
10900dede8b0SSimon J. Gerraty     char *move_target;
10913955d011SMarcel Moolenaar     static size_t cwdlen = 0;
10923955d011SMarcel Moolenaar     static size_t tmplen = 0;
10933955d011SMarcel Moolenaar     FILE *fp;
1094b0c40a00SSimon J. Gerraty     bool needOODATE = false;
109506b9b3e0SSimon J. Gerraty     StringList missingFiles;
1096b0c40a00SSimon J. Gerraty     bool have_filemon = false;
10979f45a3c8SSimon J. Gerraty     bool cmp_filter;
10983955d011SMarcel Moolenaar 
10993955d011SMarcel Moolenaar     if (oodate)
11003955d011SMarcel Moolenaar 	return oodate;		/* we're done */
11013955d011SMarcel Moolenaar 
1102dba7b0efSSimon J. Gerraty     dname = Var_Value(gn, ".OBJDIR");
1103956e45f6SSimon J. Gerraty     tname = GNode_VarTarget(gn);
1104b778b302SSimon J. Gerraty 
1105b778b302SSimon J. Gerraty     /* if this succeeds fname3 is realpath of dname */
1106b0c40a00SSimon J. Gerraty     if (!meta_needed(gn, dname.str, fname3, false))
1107b778b302SSimon J. Gerraty 	goto oodate_out;
110806b9b3e0SSimon J. Gerraty     dname.str = fname3;
1109b778b302SSimon J. Gerraty 
111006b9b3e0SSimon J. Gerraty     Lst_Init(&missingFiles);
11113955d011SMarcel Moolenaar 
11123955d011SMarcel Moolenaar     /*
11133955d011SMarcel Moolenaar      * We need to check if the target is out-of-date. This includes
11143955d011SMarcel Moolenaar      * checking if the expanded command has changed. This in turn
11153955d011SMarcel Moolenaar      * requires that all variables are set in the same way that they
11163955d011SMarcel Moolenaar      * would be if the target needs to be re-built.
11173955d011SMarcel Moolenaar      */
1118b0c40a00SSimon J. Gerraty     GNode_SetLocalVars(gn);
11193955d011SMarcel Moolenaar 
112006b9b3e0SSimon J. Gerraty     meta_name(fname, sizeof fname, dname.str, tname, dname.str);
11213955d011SMarcel Moolenaar 
11223955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
1123956e45f6SSimon J. Gerraty     DEBUG1(META, "meta_oodate: %s\n", fname);
11243955d011SMarcel Moolenaar #endif
11253955d011SMarcel Moolenaar 
11263955d011SMarcel Moolenaar     if ((fp = fopen(fname, "r")) != NULL) {
11273955d011SMarcel Moolenaar 	static char *buf = NULL;
11283955d011SMarcel Moolenaar 	static size_t bufsz;
11299f45a3c8SSimon J. Gerraty 	unsigned lineno = 0;
11303955d011SMarcel Moolenaar 	int lastpid = 0;
11313955d011SMarcel Moolenaar 	int pid;
11323955d011SMarcel Moolenaar 	int x;
1133956e45f6SSimon J. Gerraty 	StringListNode *cmdNode;
1134e2eeea75SSimon J. Gerraty 	struct cached_stat cst;
11353955d011SMarcel Moolenaar 
1136e2eeea75SSimon J. Gerraty 	if (buf == NULL) {
11373955d011SMarcel Moolenaar 	    bufsz = 8 * BUFSIZ;
11383955d011SMarcel Moolenaar 	    buf = bmake_malloc(bufsz);
11393955d011SMarcel Moolenaar 	}
11403955d011SMarcel Moolenaar 
1141e2eeea75SSimon J. Gerraty 	if (cwdlen == 0) {
1142e2eeea75SSimon J. Gerraty 	    if (getcwd(cwd, sizeof cwd) == NULL)
11433955d011SMarcel Moolenaar 		err(1, "Could not get current working directory");
11443955d011SMarcel Moolenaar 	    cwdlen = strlen(cwd);
11453955d011SMarcel Moolenaar 	}
1146e2eeea75SSimon J. Gerraty 	strlcpy(lcwd, cwd, sizeof lcwd);
1147e2eeea75SSimon J. Gerraty 	strlcpy(latestdir, cwd, sizeof latestdir);
11483955d011SMarcel Moolenaar 
1149e2eeea75SSimon J. Gerraty 	if (tmpdir == NULL) {
11503955d011SMarcel Moolenaar 	    tmpdir = getTmpdir();
11513955d011SMarcel Moolenaar 	    tmplen = strlen(tmpdir);
11523955d011SMarcel Moolenaar 	}
11533955d011SMarcel Moolenaar 
11543955d011SMarcel Moolenaar 	/* we want to track all the .meta we read */
1155dba7b0efSSimon J. Gerraty 	Global_Append(".MAKE.META.FILES", fname);
11563955d011SMarcel Moolenaar 
11571d3f2ddcSSimon J. Gerraty 	cmp_filter = metaCmpFilter || Var_Exists(gn, MAKE_META_CMP_FILTER);
11589f45a3c8SSimon J. Gerraty 
115906b9b3e0SSimon J. Gerraty 	cmdNode = gn->commands.first;
11603955d011SMarcel Moolenaar 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
11613955d011SMarcel Moolenaar 	    lineno++;
11623955d011SMarcel Moolenaar 	    if (buf[x - 1] == '\n')
11633955d011SMarcel Moolenaar 		buf[x - 1] = '\0';
11643955d011SMarcel Moolenaar 	    else {
11659f45a3c8SSimon J. Gerraty 		warnx("%s: %u: line truncated at %u", fname, lineno, x);
1166b0c40a00SSimon J. Gerraty 		oodate = true;
11673955d011SMarcel Moolenaar 		break;
11683955d011SMarcel Moolenaar 	    }
11690dede8b0SSimon J. Gerraty 	    link_src = NULL;
11700dede8b0SSimon J. Gerraty 	    move_target = NULL;
11713955d011SMarcel Moolenaar 	    /* Find the start of the build monitor section. */
1172b778b302SSimon J. Gerraty 	    if (!have_filemon) {
11733955d011SMarcel Moolenaar 		if (strncmp(buf, "-- filemon", 10) == 0) {
1174b0c40a00SSimon J. Gerraty 		    have_filemon = true;
11753955d011SMarcel Moolenaar 		    continue;
11763955d011SMarcel Moolenaar 		}
11773955d011SMarcel Moolenaar 		if (strncmp(buf, "# buildmon", 10) == 0) {
1178b0c40a00SSimon J. Gerraty 		    have_filemon = true;
11793955d011SMarcel Moolenaar 		    continue;
11803955d011SMarcel Moolenaar 		}
11813955d011SMarcel Moolenaar 	    }
11823955d011SMarcel Moolenaar 
11833955d011SMarcel Moolenaar 	    /* Delimit the record type. */
11843955d011SMarcel Moolenaar 	    p = buf;
11853955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
11869f45a3c8SSimon J. Gerraty 	    DEBUG3(META, "%s: %u: %s\n", fname, lineno, buf);
11873955d011SMarcel Moolenaar #endif
11883955d011SMarcel Moolenaar 	    strsep(&p, " ");
1189b778b302SSimon J. Gerraty 	    if (have_filemon) {
11903955d011SMarcel Moolenaar 		/*
11913955d011SMarcel Moolenaar 		 * We are in the 'filemon' output section.
11923955d011SMarcel Moolenaar 		 * Each record from filemon follows the general form:
11933955d011SMarcel Moolenaar 		 *
11943955d011SMarcel Moolenaar 		 * <key> <pid> <data>
11953955d011SMarcel Moolenaar 		 *
11963955d011SMarcel Moolenaar 		 * Where:
11973955d011SMarcel Moolenaar 		 * <key> is a single letter, denoting the syscall.
11983955d011SMarcel Moolenaar 		 * <pid> is the process that made the syscall.
11993955d011SMarcel Moolenaar 		 * <data> is the arguments (of interest).
12003955d011SMarcel Moolenaar 		 */
12013955d011SMarcel Moolenaar 		switch(buf[0]) {
12023955d011SMarcel Moolenaar 		case '#':		/* comment */
12033955d011SMarcel Moolenaar 		case 'V':		/* version */
12043955d011SMarcel Moolenaar 		    break;
12053955d011SMarcel Moolenaar 		default:
12063955d011SMarcel Moolenaar 		    /*
12073955d011SMarcel Moolenaar 		     * We need to track pathnames per-process.
12083955d011SMarcel Moolenaar 		     *
1209dba7b0efSSimon J. Gerraty 		     * Each process run by make starts off in the 'CWD'
12103955d011SMarcel Moolenaar 		     * recorded in the .meta file, if it chdirs ('C')
12113955d011SMarcel Moolenaar 		     * elsewhere we need to track that - but only for
12123955d011SMarcel Moolenaar 		     * that process.  If it forks ('F'), we initialize
12133955d011SMarcel Moolenaar 		     * the child to have the same cwd as its parent.
12143955d011SMarcel Moolenaar 		     *
12153955d011SMarcel Moolenaar 		     * We also need to track the 'latestdir' of
12163955d011SMarcel Moolenaar 		     * interest.  This is usually the same as cwd, but
12173955d011SMarcel Moolenaar 		     * not if a process is reading directories.
12183955d011SMarcel Moolenaar 		     *
12193955d011SMarcel Moolenaar 		     * Each time we spot a different process ('pid')
12203955d011SMarcel Moolenaar 		     * we save the current value of 'latestdir' in a
12213955d011SMarcel Moolenaar 		     * variable qualified by 'lastpid', and
12223955d011SMarcel Moolenaar 		     * re-initialize 'latestdir' to any pre-saved
12233955d011SMarcel Moolenaar 		     * value for the current 'pid' and 'CWD' if none.
12243955d011SMarcel Moolenaar 		     */
12253955d011SMarcel Moolenaar 		    CHECK_VALID_META(p);
12263955d011SMarcel Moolenaar 		    pid = atoi(p);
12273955d011SMarcel Moolenaar 		    if (pid > 0 && pid != lastpid) {
122806b9b3e0SSimon J. Gerraty 			FStr ldir;
12293955d011SMarcel Moolenaar 
12303955d011SMarcel Moolenaar 			if (lastpid > 0) {
1231db29cad8SSimon J. Gerraty 			    /* We need to remember these. */
12329f45a3c8SSimon J. Gerraty 			    Global_Set(lcwd_vname, lcwd);
12339f45a3c8SSimon J. Gerraty 			    Global_Set(ldir_vname, latestdir);
12343955d011SMarcel Moolenaar 			}
1235e2eeea75SSimon J. Gerraty 			snprintf(lcwd_vname, sizeof lcwd_vname, LCWD_VNAME_FMT, pid);
1236e2eeea75SSimon J. Gerraty 			snprintf(ldir_vname, sizeof ldir_vname, LDIR_VNAME_FMT, pid);
12373955d011SMarcel Moolenaar 			lastpid = pid;
1238dba7b0efSSimon J. Gerraty 			ldir = Var_Value(SCOPE_GLOBAL, ldir_vname);
123906b9b3e0SSimon J. Gerraty 			if (ldir.str != NULL) {
124006b9b3e0SSimon J. Gerraty 			    strlcpy(latestdir, ldir.str, sizeof latestdir);
124106b9b3e0SSimon J. Gerraty 			    FStr_Done(&ldir);
1242db29cad8SSimon J. Gerraty 			}
1243dba7b0efSSimon J. Gerraty 			ldir = Var_Value(SCOPE_GLOBAL, lcwd_vname);
124406b9b3e0SSimon J. Gerraty 			if (ldir.str != NULL) {
124506b9b3e0SSimon J. Gerraty 			    strlcpy(lcwd, ldir.str, sizeof lcwd);
124606b9b3e0SSimon J. Gerraty 			    FStr_Done(&ldir);
1247db29cad8SSimon J. Gerraty 			}
12483955d011SMarcel Moolenaar 		    }
12493955d011SMarcel Moolenaar 		    /* Skip past the pid. */
12503955d011SMarcel Moolenaar 		    if (strsep(&p, " ") == NULL)
12513955d011SMarcel Moolenaar 			continue;
12523955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
12533955d011SMarcel Moolenaar 		    if (DEBUG(META))
12549f45a3c8SSimon J. Gerraty 			debug_printf("%s: %u: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1255db29cad8SSimon J. Gerraty 				     fname, lineno,
1256db29cad8SSimon J. Gerraty 				     pid, buf[0], cwd, lcwd, latestdir);
12573955d011SMarcel Moolenaar #endif
12583955d011SMarcel Moolenaar 		    break;
12593955d011SMarcel Moolenaar 		}
12603955d011SMarcel Moolenaar 
12613955d011SMarcel Moolenaar 		CHECK_VALID_META(p);
12623955d011SMarcel Moolenaar 
12633955d011SMarcel Moolenaar 		/* Process according to record type. */
12643955d011SMarcel Moolenaar 		switch (buf[0]) {
12653955d011SMarcel Moolenaar 		case 'X':		/* eXit */
12669f45a3c8SSimon J. Gerraty 		    Var_Delete(SCOPE_GLOBAL, lcwd_vname);
12679f45a3c8SSimon J. Gerraty 		    Var_Delete(SCOPE_GLOBAL, ldir_vname);
12683955d011SMarcel Moolenaar 		    lastpid = 0;	/* no need to save ldir_vname */
12693955d011SMarcel Moolenaar 		    break;
12703955d011SMarcel Moolenaar 
12713955d011SMarcel Moolenaar 		case 'F':		/* [v]Fork */
12723955d011SMarcel Moolenaar 		    {
12733955d011SMarcel Moolenaar 			char cldir[64];
12743955d011SMarcel Moolenaar 			int child;
12753955d011SMarcel Moolenaar 
12763955d011SMarcel Moolenaar 			child = atoi(p);
12773955d011SMarcel Moolenaar 			if (child > 0) {
1278e2eeea75SSimon J. Gerraty 			    snprintf(cldir, sizeof cldir, LCWD_VNAME_FMT, child);
12799f45a3c8SSimon J. Gerraty 			    Global_Set(cldir, lcwd);
1280e2eeea75SSimon J. Gerraty 			    snprintf(cldir, sizeof cldir, LDIR_VNAME_FMT, child);
12819f45a3c8SSimon J. Gerraty 			    Global_Set(cldir, latestdir);
1282db29cad8SSimon J. Gerraty #ifdef DEBUG_META_MODE
1283db29cad8SSimon J. Gerraty 			    if (DEBUG(META))
1284956e45f6SSimon J. Gerraty 				debug_printf(
12859f45a3c8SSimon J. Gerraty 					"%s: %u: %d: cwd=%s lcwd=%s ldir=%s\n",
1286db29cad8SSimon J. Gerraty 					fname, lineno,
1287db29cad8SSimon J. Gerraty 					child, cwd, lcwd, latestdir);
1288db29cad8SSimon J. Gerraty #endif
12893955d011SMarcel Moolenaar 			}
12903955d011SMarcel Moolenaar 		    }
12913955d011SMarcel Moolenaar 		    break;
12923955d011SMarcel Moolenaar 
12933955d011SMarcel Moolenaar 		case 'C':		/* Chdir */
1294db29cad8SSimon J. Gerraty 		    /* Update lcwd and latest directory. */
1295e2eeea75SSimon J. Gerraty 		    strlcpy(latestdir, p, sizeof latestdir);
1296e2eeea75SSimon J. Gerraty 		    strlcpy(lcwd, p, sizeof lcwd);
12979f45a3c8SSimon J. Gerraty 		    Global_Set(lcwd_vname, lcwd);
12989f45a3c8SSimon J. Gerraty 		    Global_Set(ldir_vname, lcwd);
1299db29cad8SSimon J. Gerraty #ifdef DEBUG_META_MODE
13009f45a3c8SSimon J. Gerraty 		    DEBUG4(META, "%s: %u: cwd=%s ldir=%s\n",
1301956e45f6SSimon J. Gerraty 			   fname, lineno, cwd, lcwd);
1302db29cad8SSimon J. Gerraty #endif
13033955d011SMarcel Moolenaar 		    break;
13043955d011SMarcel Moolenaar 
13053955d011SMarcel Moolenaar 		case 'M':		/* renaMe */
13060dede8b0SSimon J. Gerraty 		    /*
13070dede8b0SSimon J. Gerraty 		     * For 'M'oves we want to check
13080dede8b0SSimon J. Gerraty 		     * the src as for 'R'ead
13090dede8b0SSimon J. Gerraty 		     * and the target as for 'W'rite.
13100dede8b0SSimon J. Gerraty 		     */
131106b9b3e0SSimon J. Gerraty 		    {
131206b9b3e0SSimon J. Gerraty 			char *cp = p;	/* save this for a second */
13130dede8b0SSimon J. Gerraty 			/* now get target */
13140dede8b0SSimon J. Gerraty 			if (strsep(&p, " ") == NULL)
13150dede8b0SSimon J. Gerraty 			    continue;
13160dede8b0SSimon J. Gerraty 			CHECK_VALID_META(p);
13170dede8b0SSimon J. Gerraty 			move_target = p;
13180dede8b0SSimon J. Gerraty 			p = cp;
131906b9b3e0SSimon J. Gerraty 		    }
13203955d011SMarcel Moolenaar 		    /* 'L' and 'M' put single quotes around the args */
13210dede8b0SSimon J. Gerraty 		    DEQUOTE(p);
13220dede8b0SSimon J. Gerraty 		    DEQUOTE(move_target);
13233955d011SMarcel Moolenaar 		    /* FALLTHROUGH */
13243955d011SMarcel Moolenaar 		case 'D':		/* unlink */
1325956e45f6SSimon J. Gerraty 		    if (*p == '/') {
132695e3ed2cSSimon J. Gerraty 			/* remove any missingFiles entries that match p */
132706b9b3e0SSimon J. Gerraty 			StringListNode *ln = missingFiles.first;
1328956e45f6SSimon J. Gerraty 			while (ln != NULL) {
1329956e45f6SSimon J. Gerraty 			    StringListNode *next = ln->next;
1330956e45f6SSimon J. Gerraty 			    if (path_starts_with(ln->datum, p)) {
1331956e45f6SSimon J. Gerraty 				free(ln->datum);
133206b9b3e0SSimon J. Gerraty 				Lst_Remove(&missingFiles, ln);
1333956e45f6SSimon J. Gerraty 			    }
1334956e45f6SSimon J. Gerraty 			    ln = next;
13353955d011SMarcel Moolenaar 			}
13363955d011SMarcel Moolenaar 		    }
13370dede8b0SSimon J. Gerraty 		    if (buf[0] == 'M') {
13380dede8b0SSimon J. Gerraty 			/* the target of the mv is a file 'W'ritten */
13390dede8b0SSimon J. Gerraty #ifdef DEBUG_META_MODE
1340956e45f6SSimon J. Gerraty 			DEBUG2(META, "meta_oodate: M %s -> %s\n",
13410dede8b0SSimon J. Gerraty 			       p, move_target);
13420dede8b0SSimon J. Gerraty #endif
13430dede8b0SSimon J. Gerraty 			p = move_target;
13440dede8b0SSimon J. Gerraty 			goto check_write;
13450dede8b0SSimon J. Gerraty 		    }
13463955d011SMarcel Moolenaar 		    break;
13473955d011SMarcel Moolenaar 		case 'L':		/* Link */
13480dede8b0SSimon J. Gerraty 		    /*
13490dede8b0SSimon J. Gerraty 		     * For 'L'inks check
13500dede8b0SSimon J. Gerraty 		     * the src as for 'R'ead
13510dede8b0SSimon J. Gerraty 		     * and the target as for 'W'rite.
13520dede8b0SSimon J. Gerraty 		     */
13530dede8b0SSimon J. Gerraty 		    link_src = p;
13540dede8b0SSimon J. Gerraty 		    /* now get target */
13553955d011SMarcel Moolenaar 		    if (strsep(&p, " ") == NULL)
13563955d011SMarcel Moolenaar 			continue;
13573955d011SMarcel Moolenaar 		    CHECK_VALID_META(p);
13583955d011SMarcel Moolenaar 		    /* 'L' and 'M' put single quotes around the args */
13590dede8b0SSimon J. Gerraty 		    DEQUOTE(p);
13600dede8b0SSimon J. Gerraty 		    DEQUOTE(link_src);
13610dede8b0SSimon J. Gerraty #ifdef DEBUG_META_MODE
1362956e45f6SSimon J. Gerraty 		    DEBUG2(META, "meta_oodate: L %s -> %s\n", link_src, p);
13630dede8b0SSimon J. Gerraty #endif
13643955d011SMarcel Moolenaar 		    /* FALLTHROUGH */
13653955d011SMarcel Moolenaar 		case 'W':		/* Write */
13660dede8b0SSimon J. Gerraty 		check_write:
13673955d011SMarcel Moolenaar 		    /*
13683955d011SMarcel Moolenaar 		     * If a file we generated within our bailiwick
13693955d011SMarcel Moolenaar 		     * but outside of .OBJDIR is missing,
13703955d011SMarcel Moolenaar 		     * we need to do it again.
13713955d011SMarcel Moolenaar 		     */
13723955d011SMarcel Moolenaar 		    /* ignore non-absolute paths */
13733955d011SMarcel Moolenaar 		    if (*p != '/')
13743955d011SMarcel Moolenaar 			break;
13753955d011SMarcel Moolenaar 
137606b9b3e0SSimon J. Gerraty 		    if (Lst_IsEmpty(&metaBailiwick))
13773955d011SMarcel Moolenaar 			break;
13783955d011SMarcel Moolenaar 
13793955d011SMarcel Moolenaar 		    /* ignore cwd - normal dependencies handle those */
13803955d011SMarcel Moolenaar 		    if (strncmp(p, cwd, cwdlen) == 0)
13813955d011SMarcel Moolenaar 			break;
13823955d011SMarcel Moolenaar 
138306b9b3e0SSimon J. Gerraty 		    if (!has_any_prefix(p, &metaBailiwick))
13843955d011SMarcel Moolenaar 			break;
13853955d011SMarcel Moolenaar 
13863955d011SMarcel Moolenaar 		    /* tmpdir might be within */
13873955d011SMarcel Moolenaar 		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
13883955d011SMarcel Moolenaar 			break;
13893955d011SMarcel Moolenaar 
13903955d011SMarcel Moolenaar 		    /* ignore anything containing the string "tmp" */
1391e2eeea75SSimon J. Gerraty 		    /* XXX: The arguments to strstr must be swapped. */
139206b9b3e0SSimon J. Gerraty 		    if (strstr("tmp", p) != NULL)
13933955d011SMarcel Moolenaar 			break;
13943955d011SMarcel Moolenaar 
1395e2eeea75SSimon J. Gerraty 		    if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
1396e2eeea75SSimon J. Gerraty 			(link_src == NULL && cached_stat(p, &cst) < 0)) {
1397956e45f6SSimon J. Gerraty 			if (!meta_ignore(gn, p))
139806b9b3e0SSimon J. Gerraty 			    append_if_new(&missingFiles, p);
139995e3ed2cSSimon J. Gerraty 		    }
14003955d011SMarcel Moolenaar 		    break;
14010dede8b0SSimon J. Gerraty 		check_link_src:
14020dede8b0SSimon J. Gerraty 		    p = link_src;
14030dede8b0SSimon J. Gerraty 		    link_src = NULL;
14040dede8b0SSimon J. Gerraty #ifdef DEBUG_META_MODE
1405956e45f6SSimon J. Gerraty 		    DEBUG1(META, "meta_oodate: L src %s\n", p);
14060dede8b0SSimon J. Gerraty #endif
14070dede8b0SSimon J. Gerraty 		    /* FALLTHROUGH */
14083955d011SMarcel Moolenaar 		case 'R':		/* Read */
14093955d011SMarcel Moolenaar 		case 'E':		/* Exec */
14103955d011SMarcel Moolenaar 		    /*
14113955d011SMarcel Moolenaar 		     * Check for runtime files that can't
14123955d011SMarcel Moolenaar 		     * be part of the dependencies because
14133955d011SMarcel Moolenaar 		     * they are _expected_ to change.
14143955d011SMarcel Moolenaar 		     */
141595e3ed2cSSimon J. Gerraty 		    if (meta_ignore(gn, p))
14163955d011SMarcel Moolenaar 			break;
14173955d011SMarcel Moolenaar 
14183955d011SMarcel Moolenaar 		    /*
14193955d011SMarcel Moolenaar 		     * The rest of the record is the file name.
14203955d011SMarcel Moolenaar 		     * Check if it's not an absolute path.
14213955d011SMarcel Moolenaar 		     */
14223955d011SMarcel Moolenaar 		    {
14233955d011SMarcel Moolenaar 			char *sdirs[4];
14243955d011SMarcel Moolenaar 			char **sdp;
14253955d011SMarcel Moolenaar 			int sdx = 0;
1426b0c40a00SSimon J. Gerraty 			bool found = false;
14273955d011SMarcel Moolenaar 
14283955d011SMarcel Moolenaar 			if (*p == '/') {
14293955d011SMarcel Moolenaar 			    sdirs[sdx++] = p; /* done */
14303955d011SMarcel Moolenaar 			} else {
14313955d011SMarcel Moolenaar 			    if (strcmp(".", p) == 0)
14323955d011SMarcel Moolenaar 				continue; /* no point */
14333955d011SMarcel Moolenaar 
14343955d011SMarcel Moolenaar 			    /* Check vs latestdir */
14354fde40d9SSimon J. Gerraty 			    if (snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p) < (int)(sizeof fname1))
14363955d011SMarcel Moolenaar 				sdirs[sdx++] = fname1;
14373955d011SMarcel Moolenaar 
1438db29cad8SSimon J. Gerraty 			    if (strcmp(latestdir, lcwd) != 0) {
1439db29cad8SSimon J. Gerraty 				/* Check vs lcwd */
14404fde40d9SSimon J. Gerraty 				if (snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p) < (int)(sizeof fname2))
14413955d011SMarcel Moolenaar 				    sdirs[sdx++] = fname2;
14423955d011SMarcel Moolenaar 			    }
1443db29cad8SSimon J. Gerraty 			    if (strcmp(lcwd, cwd) != 0) {
1444db29cad8SSimon J. Gerraty 				/* Check vs cwd */
14454fde40d9SSimon J. Gerraty 				if (snprintf(fname3, sizeof fname3, "%s/%s", cwd, p) < (int)(sizeof fname3))
1446db29cad8SSimon J. Gerraty 				    sdirs[sdx++] = fname3;
1447db29cad8SSimon J. Gerraty 			    }
14483955d011SMarcel Moolenaar 			}
14493955d011SMarcel Moolenaar 			sdirs[sdx++] = NULL;
14503955d011SMarcel Moolenaar 
145106b9b3e0SSimon J. Gerraty 			for (sdp = sdirs; *sdp != NULL && !found; sdp++) {
14523955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
14539f45a3c8SSimon J. Gerraty 			    DEBUG3(META, "%s: %u: looking for: %s\n",
1454956e45f6SSimon J. Gerraty 				   fname, lineno, *sdp);
14553955d011SMarcel Moolenaar #endif
1456e2eeea75SSimon J. Gerraty 			    if (cached_stat(*sdp, &cst) == 0) {
1457b0c40a00SSimon J. Gerraty 				found = true;
14583955d011SMarcel Moolenaar 				p = *sdp;
14593955d011SMarcel Moolenaar 			    }
14603955d011SMarcel Moolenaar 			}
14613955d011SMarcel Moolenaar 			if (found) {
14623955d011SMarcel Moolenaar #ifdef DEBUG_META_MODE
14639f45a3c8SSimon J. Gerraty 			    DEBUG3(META, "%s: %u: found: %s\n",
1464956e45f6SSimon J. Gerraty 				   fname, lineno, p);
14653955d011SMarcel Moolenaar #endif
1466e2eeea75SSimon J. Gerraty 			    if (!S_ISDIR(cst.cst_mode) &&
1467e2eeea75SSimon J. Gerraty 				cst.cst_mtime > gn->mtime) {
14689f45a3c8SSimon J. Gerraty 				DEBUG3(META, "%s: %u: file '%s' is newer than the target...\n",
1469956e45f6SSimon J. Gerraty 				       fname, lineno, p);
1470b0c40a00SSimon J. Gerraty 				oodate = true;
1471e2eeea75SSimon J. Gerraty 			    } else if (S_ISDIR(cst.cst_mode)) {
14723955d011SMarcel Moolenaar 				/* Update the latest directory. */
1473b778b302SSimon J. Gerraty 				cached_realpath(p, latestdir);
14743955d011SMarcel Moolenaar 			    }
14753955d011SMarcel Moolenaar 			} else if (errno == ENOENT && *p == '/' &&
14763955d011SMarcel Moolenaar 				   strncmp(p, cwd, cwdlen) != 0) {
14773955d011SMarcel Moolenaar 			    /*
14783955d011SMarcel Moolenaar 			     * A referenced file outside of CWD is missing.
14793955d011SMarcel Moolenaar 			     * We cannot catch every eventuality here...
14803955d011SMarcel Moolenaar 			     */
148106b9b3e0SSimon J. Gerraty 			    append_if_new(&missingFiles, p);
14823955d011SMarcel Moolenaar 			}
14833955d011SMarcel Moolenaar 		    }
1484db29cad8SSimon J. Gerraty 		    if (buf[0] == 'E') {
1485db29cad8SSimon J. Gerraty 			/* previous latestdir is no longer relevant */
1486e2eeea75SSimon J. Gerraty 			strlcpy(latestdir, lcwd, sizeof latestdir);
1487db29cad8SSimon J. Gerraty 		    }
14883955d011SMarcel Moolenaar 		    break;
14893955d011SMarcel Moolenaar 		default:
14903955d011SMarcel Moolenaar 		    break;
14913955d011SMarcel Moolenaar 		}
14920dede8b0SSimon J. Gerraty 		if (!oodate && buf[0] == 'L' && link_src != NULL)
14930dede8b0SSimon J. Gerraty 		    goto check_link_src;
14943955d011SMarcel Moolenaar 	    } else if (strcmp(buf, "CMD") == 0) {
14953955d011SMarcel Moolenaar 		/*
14963955d011SMarcel Moolenaar 		 * Compare the current command with the one in the
14973955d011SMarcel Moolenaar 		 * meta data file.
14983955d011SMarcel Moolenaar 		 */
1499956e45f6SSimon J. Gerraty 		if (cmdNode == NULL) {
15009f45a3c8SSimon J. Gerraty 		    DEBUG2(META, "%s: %u: there were more build commands in the meta data file than there are now...\n",
1501956e45f6SSimon J. Gerraty 			   fname, lineno);
1502b0c40a00SSimon J. Gerraty 		    oodate = true;
15033955d011SMarcel Moolenaar 		} else {
150406b9b3e0SSimon J. Gerraty 		    const char *cp;
1505956e45f6SSimon J. Gerraty 		    char *cmd = cmdNode->datum;
1506b0c40a00SSimon J. Gerraty 		    bool hasOODATE = false;
15073955d011SMarcel Moolenaar 
150806b9b3e0SSimon J. Gerraty 		    if (strstr(cmd, "$?") != NULL)
1509b0c40a00SSimon J. Gerraty 			hasOODATE = true;
151006b9b3e0SSimon J. Gerraty 		    else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
15111748de26SSimon J. Gerraty 			/* check for $[{(].OODATE[:)}] */
15123955d011SMarcel Moolenaar 			if (cp > cmd + 2 && cp[-2] == '$')
1513b0c40a00SSimon J. Gerraty 			    hasOODATE = true;
15143955d011SMarcel Moolenaar 		    }
15151748de26SSimon J. Gerraty 		    if (hasOODATE) {
1516b0c40a00SSimon J. Gerraty 			needOODATE = true;
15179f45a3c8SSimon J. Gerraty 			DEBUG2(META, "%s: %u: cannot compare command using .OODATE\n",
1518956e45f6SSimon J. Gerraty 			       fname, lineno);
15193955d011SMarcel Moolenaar 		    }
15208d5c8e21SSimon J. Gerraty 		    cmd = Var_Subst(cmd, gn, VARE_EVAL_DEFINED);
1521956e45f6SSimon J. Gerraty 		    /* TODO: handle errors */
15223955d011SMarcel Moolenaar 
152306b9b3e0SSimon J. Gerraty 		    if ((cp = strchr(cmd, '\n')) != NULL) {
15243955d011SMarcel Moolenaar 			int n;
15253955d011SMarcel Moolenaar 
15263955d011SMarcel Moolenaar 			/*
15273955d011SMarcel Moolenaar 			 * This command contains newlines, we need to
15283955d011SMarcel Moolenaar 			 * fetch more from the .meta file before we
15293955d011SMarcel Moolenaar 			 * attempt a comparison.
15303955d011SMarcel Moolenaar 			 */
15313955d011SMarcel Moolenaar 			/* first put the newline back at buf[x - 1] */
15323955d011SMarcel Moolenaar 			buf[x - 1] = '\n';
15333955d011SMarcel Moolenaar 			do {
15343955d011SMarcel Moolenaar 			    /* now fetch the next line */
15353955d011SMarcel Moolenaar 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
15363955d011SMarcel Moolenaar 				break;
15373955d011SMarcel Moolenaar 			    x = n;
15383955d011SMarcel Moolenaar 			    lineno++;
15393955d011SMarcel Moolenaar 			    if (buf[x - 1] != '\n') {
15409f45a3c8SSimon J. Gerraty 				warnx("%s: %u: line truncated at %u", fname, lineno, x);
15413955d011SMarcel Moolenaar 				break;
15423955d011SMarcel Moolenaar 			    }
154306b9b3e0SSimon J. Gerraty 			    cp = strchr(cp + 1, '\n');
154406b9b3e0SSimon J. Gerraty 			} while (cp != NULL);
15453955d011SMarcel Moolenaar 			if (buf[x - 1] == '\n')
15463955d011SMarcel Moolenaar 			    buf[x - 1] = '\0';
15473955d011SMarcel Moolenaar 		    }
1548e2eeea75SSimon J. Gerraty 		    if (p != NULL &&
154949caa483SSimon J. Gerraty 			!hasOODATE &&
15503955d011SMarcel Moolenaar 			!(gn->type & OP_NOMETA_CMP) &&
15519f45a3c8SSimon J. Gerraty 			(meta_cmd_cmp(gn, p, cmd, cmp_filter) != 0)) {
15529f45a3c8SSimon J. Gerraty 			DEBUG4(META, "%s: %u: a build command has changed\n%s\nvs\n%s\n",
1553956e45f6SSimon J. Gerraty 			       fname, lineno, p, cmd);
15543955d011SMarcel Moolenaar 			if (!metaIgnoreCMDs)
1555b0c40a00SSimon J. Gerraty 			    oodate = true;
15563955d011SMarcel Moolenaar 		    }
15573955d011SMarcel Moolenaar 		    free(cmd);
1558956e45f6SSimon J. Gerraty 		    cmdNode = cmdNode->next;
15593955d011SMarcel Moolenaar 		}
15603955d011SMarcel Moolenaar 	    } else if (strcmp(buf, "CWD") == 0) {
15613955d011SMarcel Moolenaar 		/*
15623955d011SMarcel Moolenaar 		 * Check if there are extra commands now
15633955d011SMarcel Moolenaar 		 * that weren't in the meta data file.
15643955d011SMarcel Moolenaar 		 */
1565956e45f6SSimon J. Gerraty 		if (!oodate && cmdNode != NULL) {
15669f45a3c8SSimon J. Gerraty 		    DEBUG2(META, "%s: %u: there are extra build commands now that weren't in the meta data file\n",
1567956e45f6SSimon J. Gerraty 			   fname, lineno);
1568b0c40a00SSimon J. Gerraty 		    oodate = true;
15693955d011SMarcel Moolenaar 		}
157049caa483SSimon J. Gerraty 		CHECK_VALID_META(p);
15713955d011SMarcel Moolenaar 		if (strcmp(p, cwd) != 0) {
15729f45a3c8SSimon J. Gerraty 		    DEBUG4(META, "%s: %u: the current working directory has changed from '%s' to '%s'\n",
1573956e45f6SSimon J. Gerraty 			   fname, lineno, p, curdir);
1574b0c40a00SSimon J. Gerraty 		    oodate = true;
15753955d011SMarcel Moolenaar 		}
15763955d011SMarcel Moolenaar 	    }
15773955d011SMarcel Moolenaar 	}
15783955d011SMarcel Moolenaar 
15793955d011SMarcel Moolenaar 	fclose(fp);
158006b9b3e0SSimon J. Gerraty 	if (!Lst_IsEmpty(&missingFiles)) {
1581956e45f6SSimon J. Gerraty 	    DEBUG2(META, "%s: missing files: %s...\n",
158206b9b3e0SSimon J. Gerraty 		   fname, (char *)missingFiles.first->datum);
1583b0c40a00SSimon J. Gerraty 	    oodate = true;
15843955d011SMarcel Moolenaar 	}
1585b778b302SSimon J. Gerraty 	if (!oodate && !have_filemon && filemonMissing) {
1586956e45f6SSimon J. Gerraty 	    DEBUG1(META, "%s: missing filemon data\n", fname);
1587b0c40a00SSimon J. Gerraty 	    oodate = true;
1588b778b302SSimon J. Gerraty 	}
15893955d011SMarcel Moolenaar     } else {
15903841c287SSimon J. Gerraty 	if (writeMeta && (metaMissing || (gn->type & OP_META))) {
159106b9b3e0SSimon J. Gerraty 	    const char *cp = NULL;
1592b778b302SSimon J. Gerraty 
1593b778b302SSimon J. Gerraty 	    /* if target is in .CURDIR we do not need a meta file */
159406b9b3e0SSimon J. Gerraty 	    if (gn->path != NULL && (cp = strrchr(gn->path, '/')) != NULL &&
159506b9b3e0SSimon J. Gerraty 		(cp > gn->path)) {
1596956e45f6SSimon J. Gerraty 		if (strncmp(curdir, gn->path, (size_t)(cp - gn->path)) != 0) {
1597b778b302SSimon J. Gerraty 		    cp = NULL;		/* not in .CURDIR */
1598b778b302SSimon J. Gerraty 		}
1599b778b302SSimon J. Gerraty 	    }
1600e2eeea75SSimon J. Gerraty 	    if (cp == NULL) {
1601956e45f6SSimon J. Gerraty 		DEBUG1(META, "%s: required but missing\n", fname);
1602b0c40a00SSimon J. Gerraty 		oodate = true;
1603b0c40a00SSimon J. Gerraty 		needOODATE = true;	/* assume the worst */
16043955d011SMarcel Moolenaar 	    }
16053955d011SMarcel Moolenaar 	}
1606b778b302SSimon J. Gerraty     }
1607be19d90bSSimon J. Gerraty 
1608548bfc56SSimon J. Gerraty     Lst_DoneFree(&missingFiles);
1609be19d90bSSimon J. Gerraty 
16103cbdda60SSimon J. Gerraty     if (oodate && needOODATE) {
16113955d011SMarcel Moolenaar 	/*
16123cbdda60SSimon J. Gerraty 	 * Target uses .OODATE which is empty; or we wouldn't be here.
16133cbdda60SSimon J. Gerraty 	 * We have decided it is oodate, so .OODATE needs to be set.
16143cbdda60SSimon J. Gerraty 	 * All we can sanely do is set it to .ALLSRC.
16153955d011SMarcel Moolenaar 	 */
1616dba7b0efSSimon J. Gerraty 	Var_Delete(gn, OODATE);
1617dba7b0efSSimon J. Gerraty 	Var_Set(gn, OODATE, GNode_VarAllsrc(gn));
16183955d011SMarcel Moolenaar     }
1619b778b302SSimon J. Gerraty 
1620b778b302SSimon J. Gerraty  oodate_out:
162106b9b3e0SSimon J. Gerraty     FStr_Done(&dname);
16223955d011SMarcel Moolenaar     return oodate;
16233955d011SMarcel Moolenaar }
16243955d011SMarcel Moolenaar 
16253955d011SMarcel Moolenaar /* support for compat mode */
16263955d011SMarcel Moolenaar 
16273955d011SMarcel Moolenaar static int childPipe[2];
16283955d011SMarcel Moolenaar 
16293955d011SMarcel Moolenaar void
meta_compat_start(void)16303955d011SMarcel Moolenaar meta_compat_start(void)
16313955d011SMarcel Moolenaar {
16323955d011SMarcel Moolenaar #ifdef USE_FILEMON_ONCE
16333955d011SMarcel Moolenaar     /*
16343955d011SMarcel Moolenaar      * We need to re-open filemon for each cmd.
16353955d011SMarcel Moolenaar      */
16363955d011SMarcel Moolenaar     BuildMon *pbm = &Mybm;
16373955d011SMarcel Moolenaar 
16383955d011SMarcel Moolenaar     if (pbm->mfp != NULL && useFilemon) {
163949caa483SSimon J. Gerraty 	meta_open_filemon(pbm);
16403955d011SMarcel Moolenaar     } else {
164149caa483SSimon J. Gerraty 	pbm->mon_fd = -1;
164249caa483SSimon J. Gerraty 	pbm->filemon = NULL;
16433955d011SMarcel Moolenaar     }
16443955d011SMarcel Moolenaar #endif
16453955d011SMarcel Moolenaar     if (pipe(childPipe) < 0)
16463955d011SMarcel Moolenaar 	Punt("Cannot create pipe: %s", strerror(errno));
16473955d011SMarcel Moolenaar     /* Set close-on-exec flag for both */
1648be19d90bSSimon J. Gerraty     (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1649be19d90bSSimon J. Gerraty     (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
16503955d011SMarcel Moolenaar }
16513955d011SMarcel Moolenaar 
16523955d011SMarcel Moolenaar void
meta_compat_child(void)16533955d011SMarcel Moolenaar meta_compat_child(void)
16543955d011SMarcel Moolenaar {
16553955d011SMarcel Moolenaar     meta_job_child(NULL);
16568d5c8e21SSimon J. Gerraty     if (dup2(childPipe[1], STDOUT_FILENO) < 0
16578d5c8e21SSimon J. Gerraty 	    || dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
1658956e45f6SSimon J. Gerraty 	execDie("dup2", "pipe");
16593955d011SMarcel Moolenaar }
16603955d011SMarcel Moolenaar 
16613955d011SMarcel Moolenaar void
meta_compat_parent(pid_t child)166249caa483SSimon J. Gerraty meta_compat_parent(pid_t child)
16633955d011SMarcel Moolenaar {
166449caa483SSimon J. Gerraty     int outfd, metafd, maxfd, nfds;
166549caa483SSimon J. Gerraty     char buf[BUFSIZ+1];
166649caa483SSimon J. Gerraty     fd_set readfds;
16673955d011SMarcel Moolenaar 
166849caa483SSimon J. Gerraty     meta_job_parent(NULL, child);
16693955d011SMarcel Moolenaar     close(childPipe[1]);			/* child side */
167049caa483SSimon J. Gerraty     outfd = childPipe[0];
167149caa483SSimon J. Gerraty #ifdef USE_FILEMON
167206b9b3e0SSimon J. Gerraty     metafd = Mybm.filemon != NULL ? filemon_readfd(Mybm.filemon) : -1;
167349caa483SSimon J. Gerraty #else
167449caa483SSimon J. Gerraty     metafd = -1;
167549caa483SSimon J. Gerraty #endif
167649caa483SSimon J. Gerraty     maxfd = -1;
167749caa483SSimon J. Gerraty     if (outfd > maxfd)
167849caa483SSimon J. Gerraty 	    maxfd = outfd;
167949caa483SSimon J. Gerraty     if (metafd > maxfd)
168049caa483SSimon J. Gerraty 	    maxfd = metafd;
168149caa483SSimon J. Gerraty 
168249caa483SSimon J. Gerraty     while (outfd != -1 || metafd != -1) {
168349caa483SSimon J. Gerraty 	FD_ZERO(&readfds);
168449caa483SSimon J. Gerraty 	if (outfd != -1) {
168549caa483SSimon J. Gerraty 	    FD_SET(outfd, &readfds);
16863955d011SMarcel Moolenaar 	}
168749caa483SSimon J. Gerraty 	if (metafd != -1) {
168849caa483SSimon J. Gerraty 	    FD_SET(metafd, &readfds);
168949caa483SSimon J. Gerraty 	}
169049caa483SSimon J. Gerraty 	nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
169149caa483SSimon J. Gerraty 	if (nfds == -1) {
169249caa483SSimon J. Gerraty 	    if (errno == EINTR)
169349caa483SSimon J. Gerraty 		continue;
169449caa483SSimon J. Gerraty 	    err(1, "select");
169549caa483SSimon J. Gerraty 	}
169649caa483SSimon J. Gerraty 
169706b9b3e0SSimon J. Gerraty 	if (outfd != -1 && FD_ISSET(outfd, &readfds) != 0) do {
169849caa483SSimon J. Gerraty 	    /* XXX this is not line-buffered */
1699e2eeea75SSimon J. Gerraty 	    ssize_t nread = read(outfd, buf, sizeof buf - 1);
170049caa483SSimon J. Gerraty 	    if (nread == -1)
170149caa483SSimon J. Gerraty 		err(1, "read");
170249caa483SSimon J. Gerraty 	    if (nread == 0) {
170349caa483SSimon J. Gerraty 		close(outfd);
170449caa483SSimon J. Gerraty 		outfd = -1;
170549caa483SSimon J. Gerraty 		break;
170649caa483SSimon J. Gerraty 	    }
170749caa483SSimon J. Gerraty 	    fwrite(buf, 1, (size_t)nread, stdout);
170849caa483SSimon J. Gerraty 	    fflush(stdout);
170949caa483SSimon J. Gerraty 	    buf[nread] = '\0';
171049caa483SSimon J. Gerraty 	    meta_job_output(NULL, buf, "");
171112904384SSimon J. Gerraty 	} while (false);
171206b9b3e0SSimon J. Gerraty 	if (metafd != -1 && FD_ISSET(metafd, &readfds) != 0) {
171349caa483SSimon J. Gerraty 	    if (meta_job_event(NULL) <= 0)
171449caa483SSimon J. Gerraty 		metafd = -1;
171549caa483SSimon J. Gerraty 	}
171649caa483SSimon J. Gerraty     }
17173955d011SMarcel Moolenaar }
17183955d011SMarcel Moolenaar 
17193955d011SMarcel Moolenaar #endif /* USE_META */
1720