Home | History | Annotate | Line # | Download | only in mkdep
mkdep.c revision 1.44
      1  1.44  christos /* $NetBSD: mkdep.c,v 1.44 2015/06/16 22:54:10 christos Exp $ */
      2   1.1      tron 
      3   1.1      tron /*-
      4   1.1      tron  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1      tron  * All rights reserved.
      6   1.1      tron  *
      7   1.1      tron  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      tron  * by Matthias Scheler.
      9   1.1      tron  *
     10   1.1      tron  * Redistribution and use in source and binary forms, with or without
     11   1.1      tron  * modification, are permitted provided that the following conditions
     12   1.1      tron  * are met:
     13   1.1      tron  * 1. Redistributions of source code must retain the above copyright
     14   1.1      tron  *    notice, this list of conditions and the following disclaimer.
     15   1.1      tron  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      tron  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      tron  *    documentation and/or other materials provided with the distribution.
     18   1.1      tron  *
     19   1.1      tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.8       cgd  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      tron  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      tron  */
     31   1.1      tron 
     32  1.17     lukem #if HAVE_NBTOOL_CONFIG_H
     33  1.17     lukem #include "nbtool_config.h"
     34  1.17     lukem #endif
     35  1.17     lukem 
     36   1.1      tron #include <sys/cdefs.h>
     37  1.17     lukem #if !defined(lint)
     38  1.32     lukem __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\
     39  1.32     lukem  All rights reserved.");
     40  1.44  christos __RCSID("$NetBSD: mkdep.c,v 1.44 2015/06/16 22:54:10 christos Exp $");
     41   1.1      tron #endif /* not lint */
     42  1.10        tv 
     43  1.18       dsl #include <sys/mman.h>
     44   1.1      tron #include <sys/param.h>
     45   1.1      tron #include <sys/wait.h>
     46   1.1      tron #include <ctype.h>
     47  1.10        tv #include <err.h>
     48  1.18       dsl #include <fcntl.h>
     49  1.36  christos #include <getopt.h>
     50   1.1      tron #include <locale.h>
     51   1.1      tron #include <paths.h>
     52   1.1      tron #include <stdio.h>
     53   1.1      tron #include <stdlib.h>
     54   1.1      tron #include <string.h>
     55   1.1      tron #include <unistd.h>
     56   1.1      tron 
     57  1.11    simonb #include "findcc.h"
     58  1.11    simonb 
     59  1.21       dsl typedef struct opt opt_t;
     60  1.21       dsl struct opt {
     61  1.21       dsl 	opt_t	*left;
     62  1.21       dsl 	opt_t	*right;
     63  1.21       dsl 	int	len;
     64  1.21       dsl 	int	count;
     65  1.21       dsl 	char	name[4];
     66  1.21       dsl };
     67  1.21       dsl 
     68  1.37  christos typedef struct suff_list {
     69  1.33     lukem 	size_t	len;
     70  1.37  christos 	char	*suff;
     71  1.37  christos 	struct suff_list *next;
     72  1.22       dsl } suff_list_t;
     73  1.22       dsl 
     74  1.21       dsl /* tree of includes for -o processing */
     75  1.40     joerg static opt_t *opt;
     76  1.40     joerg static int width;
     77  1.42  christos static int verbose;
     78  1.21       dsl 
     79   1.1      tron #define DEFAULT_PATH		_PATH_DEFPATH
     80   1.1      tron #define DEFAULT_FILENAME	".depend"
     81   1.1      tron 
     82  1.21       dsl static void save_for_optional(const char *, const char *);
     83  1.42  christos static size_t write_optional(int, opt_t *, size_t);
     84  1.21       dsl 
     85  1.18       dsl static inline void *
     86  1.18       dsl deconst(const void *p)
     87  1.18       dsl {
     88  1.18       dsl 	return (const char *)p - (const char *)0 + (char *)0;
     89  1.18       dsl }
     90   1.1      tron 
     91  1.40     joerg __dead static void
     92  1.18       dsl usage(void)
     93   1.1      tron {
     94   1.1      tron 	(void)fprintf(stderr,
     95  1.42  christos 	    "usage: %s [-aDdiopqv] [-f file] [-P prefix] [-s suffixes] "
     96  1.41      jmmv 	    "-- [flags] file ...\n",
     97   1.6       cgd 	    getprogname());
     98   1.1      tron 	exit(EXIT_FAILURE);
     99   1.1      tron }
    100   1.1      tron 
    101  1.18       dsl static int
    102  1.18       dsl run_cc(int argc, char **argv, const char **fname)
    103  1.12   msaitoh {
    104  1.28  christos 	const char *CC, *tmpdir;
    105  1.29  christos 	char * volatile pathname;
    106  1.18       dsl 	static char tmpfilename[MAXPATHLEN];
    107  1.18       dsl 	char **args;
    108  1.18       dsl 	int tmpfd;
    109  1.18       dsl 	pid_t pid, cpid;
    110  1.18       dsl 	int status;
    111   1.1      tron 
    112   1.1      tron 	if ((CC = getenv("CC")) == NULL)
    113   1.1      tron 		CC = DEFAULT_CC;
    114   1.1      tron 	if ((pathname = findcc(CC)) == NULL)
    115   1.1      tron 		if (!setenv("PATH", DEFAULT_PATH, 1))
    116   1.1      tron 			pathname = findcc(CC);
    117  1.18       dsl 	if (pathname == NULL)
    118  1.18       dsl 		err(EXIT_FAILURE, "%s: not found", CC);
    119  1.18       dsl 	if ((args = malloc((argc + 3) * sizeof(char *))) == NULL)
    120  1.18       dsl 		err(EXIT_FAILURE, "malloc");
    121   1.1      tron 
    122  1.18       dsl 	args[0] = deconst(CC);
    123  1.18       dsl 	args[1] = deconst("-M");
    124   1.1      tron 	(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
    125   1.1      tron 
    126   1.4    kleink 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    127   1.4    kleink 		tmpdir = _PATH_TMP;
    128   1.4    kleink 	(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
    129   1.4    kleink 	    "mkdepXXXXXX");
    130  1.37  christos 	if ((tmpfd = mkstemp(tmpfilename)) < 0)
    131  1.37  christos 		err(EXIT_FAILURE,  "Unable to create temporary file %s",
    132  1.37  christos 		    tmpfilename);
    133  1.18       dsl 	(void)unlink(tmpfilename);
    134  1.18       dsl 	*fname = tmpfilename;
    135   1.1      tron 
    136  1.42  christos 	if (verbose) {
    137  1.42  christos 		char **a;
    138  1.42  christos 		for (a = args; *a; a++)
    139  1.42  christos 			printf("%s ", *a);
    140  1.42  christos 		printf("\n");
    141  1.42  christos 	}
    142  1.42  christos 
    143   1.1      tron 	switch (cpid = vfork()) {
    144   1.1      tron 	case 0:
    145   1.7       cgd 		(void)dup2(tmpfd, STDOUT_FILENO);
    146   1.7       cgd 		(void)close(tmpfd);
    147   1.1      tron 
    148   1.7       cgd 		(void)execv(pathname, args);
    149   1.7       cgd 		_exit(EXIT_FAILURE);
    150   1.7       cgd 		/* NOTREACHED */
    151   1.1      tron 
    152   1.1      tron 	case -1:
    153  1.18       dsl 		err(EXIT_FAILURE, "unable to fork");
    154   1.1      tron 	}
    155   1.1      tron 
    156  1.27      elad 	free(pathname);
    157  1.27      elad 	free(args);
    158  1.27      elad 
    159   1.7       cgd 	while (((pid = wait(&status)) != cpid) && (pid >= 0))
    160   1.7       cgd 		continue;
    161   1.1      tron 
    162  1.18       dsl 	if (status)
    163  1.18       dsl 		errx(EXIT_FAILURE, "compile failed.");
    164  1.18       dsl 
    165  1.18       dsl 	return tmpfd;
    166  1.18       dsl }
    167  1.18       dsl 
    168  1.25       dsl static const char *
    169  1.25       dsl read_fname(void)
    170  1.25       dsl {
    171  1.25       dsl 	static char *fbuf;
    172  1.25       dsl 	static int fbuflen;
    173  1.25       dsl 	int len, ch;
    174  1.25       dsl 
    175  1.25       dsl 	for (len = 0; (ch = getchar()) != EOF; len++) {
    176  1.25       dsl 		if (isspace(ch)) {
    177  1.25       dsl 			if (len != 0)
    178  1.25       dsl 				break;
    179  1.25       dsl 			len--;
    180  1.25       dsl 			continue;
    181  1.25       dsl 		}
    182  1.25       dsl 		if (len >= fbuflen - 1) {
    183  1.25       dsl 			fbuf = realloc(fbuf, fbuflen += 32);
    184  1.25       dsl 			if (fbuf == NULL)
    185  1.25       dsl 				err(EXIT_FAILURE, "no memory");
    186  1.25       dsl 		}
    187  1.25       dsl 		fbuf[len] = ch;
    188  1.25       dsl 	}
    189  1.25       dsl 	if (len == 0)
    190  1.25       dsl 		return NULL;
    191  1.25       dsl 	fbuf[len] = 0;
    192  1.25       dsl 	return fbuf;
    193  1.25       dsl }
    194  1.25       dsl 
    195  1.36  christos static struct option longopt[] = {
    196  1.36  christos 	{ "sysroot", 1, NULL, 'R' },
    197  1.36  christos 	{ NULL, 0, NULL, '\0' },
    198  1.36  christos };
    199  1.36  christos 
    200  1.37  christos static void
    201  1.37  christos addsuff(suff_list_t **l, const char *s, size_t len)
    202  1.37  christos {
    203  1.37  christos 	suff_list_t *p = calloc(1, sizeof(*p));
    204  1.37  christos 	if (p == NULL)
    205  1.37  christos 		err(1, "calloc");
    206  1.37  christos 	p->suff = malloc(len + 1);
    207  1.37  christos 	if (p->suff == NULL)
    208  1.37  christos 		err(1, "malloc");
    209  1.37  christos 	memcpy(p->suff, s, len);
    210  1.37  christos 	p->suff[len] = '\0';
    211  1.37  christos 	p->len = len;
    212  1.37  christos 	p->next = *l;
    213  1.37  christos 	*l = p;
    214  1.37  christos }
    215  1.37  christos 
    216  1.18       dsl int
    217  1.18       dsl main(int argc, char **argv)
    218  1.18       dsl {
    219  1.42  christos 	int 	aflag, dflag, iflag, oflag, qflag;
    220  1.18       dsl 	const char *filename;
    221  1.18       dsl 	int	dependfile;
    222  1.20       dsl 	char	*buf, *lim, *ptr, *line, *suf, *colon, *eol;
    223  1.18       dsl 	int	ok_ind, ch;
    224  1.33     lukem 	size_t	sz;
    225  1.18       dsl 	int	fd;
    226  1.34  christos 	size_t  slen;
    227  1.18       dsl 	const char *fname;
    228  1.41      jmmv 	const char *prefix = NULL;
    229  1.22       dsl 	const char *suffixes = NULL, *s;
    230  1.22       dsl 	suff_list_t *suff_list = NULL, *sl;
    231  1.18       dsl 
    232  1.24        he 	suf = NULL;		/* XXXGCC -Wuninitialized [sun2] */
    233  1.24        he 	sl = NULL;		/* XXXGCC -Wuninitialized [sun2] */
    234  1.24        he 
    235  1.18       dsl 	setlocale(LC_ALL, "");
    236  1.18       dsl 	setprogname(argv[0]);
    237   1.1      tron 
    238  1.18       dsl 	aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC;
    239  1.18       dsl 	dflag = 0;
    240  1.42  christos 	iflag = 0;
    241  1.18       dsl 	oflag = 0;
    242  1.18       dsl 	qflag = 0;
    243  1.18       dsl 	filename = DEFAULT_FILENAME;
    244  1.18       dsl 	dependfile = -1;
    245   1.1      tron 
    246  1.18       dsl 	opterr = 0;	/* stop getopt() bleating about errors. */
    247  1.20       dsl 	for (;;) {
    248  1.20       dsl 		ok_ind = optind;
    249  1.42  christos 		ch = getopt_long(argc, argv, "aDdf:ioP:pqRs:v", longopt, NULL);
    250  1.18       dsl 		switch (ch) {
    251  1.20       dsl 		case -1:
    252  1.20       dsl 			ok_ind = optind;
    253  1.20       dsl 			break;
    254  1.18       dsl 		case 'a':	/* Append to output file */
    255  1.18       dsl 			aflag &= ~O_TRUNC;
    256  1.18       dsl 			continue;
    257  1.25       dsl 		case 'D':	/* Process *.d files (don't run cc -M) */
    258  1.25       dsl 			dflag = 2;	/* Read names from stdin */
    259  1.25       dsl 			opterr = 1;
    260  1.25       dsl 			continue;
    261  1.18       dsl 		case 'd':	/* Process *.d files (don't run cc -M) */
    262  1.18       dsl 			dflag = 1;
    263  1.18       dsl 			opterr = 1;
    264  1.18       dsl 			continue;
    265  1.18       dsl 		case 'f':	/* Name of output file */
    266  1.18       dsl 			filename = optarg;
    267  1.18       dsl 			continue;
    268  1.42  christos 		case 'i':
    269  1.42  christos 			iflag = 1;
    270  1.42  christos 			continue;
    271  1.39       wiz 		case 'o':	/* Mark dependent files .OPTIONAL */
    272  1.18       dsl 			oflag = 1;
    273  1.18       dsl 			continue;
    274  1.41      jmmv 		case 'P':	/* Prefix for each target filename */
    275  1.41      jmmv 			prefix = optarg;
    276  1.41      jmmv 			continue;
    277  1.18       dsl 		case 'p':	/* Program mode (x.o: -> x:) */
    278  1.18       dsl 			suffixes = "";
    279  1.18       dsl 			continue;
    280  1.18       dsl 		case 'q':	/* Quiet */
    281  1.18       dsl 			qflag = 1;
    282  1.18       dsl 			continue;
    283  1.36  christos 		case 'R':
    284  1.36  christos 			/* sysroot = optarg */
    285  1.36  christos 			continue;
    286  1.18       dsl 		case 's':	/* Suffix list */
    287  1.18       dsl 			suffixes = optarg;
    288  1.18       dsl 			continue;
    289  1.42  christos 		case 'v':
    290  1.42  christos 			verbose = 1;
    291  1.42  christos 			continue;
    292  1.18       dsl 		default:
    293  1.18       dsl 			if (dflag)
    294  1.18       dsl 				usage();
    295  1.18       dsl 			/* Unknown arguments are passed to "${CC} -M" */
    296  1.18       dsl 			break;
    297  1.18       dsl 		}
    298  1.18       dsl 		break;
    299   1.1      tron 	}
    300   1.1      tron 
    301  1.18       dsl 	argc -= ok_ind;
    302  1.18       dsl 	argv += ok_ind;
    303  1.25       dsl 	if ((argc == 0 && !dflag) || (argc != 0 && dflag == 2))
    304  1.18       dsl 		usage();
    305   1.1      tron 
    306  1.22       dsl 	if (suffixes != NULL) {
    307  1.37  christos 		if (*suffixes) {
    308  1.37  christos 			for (s = suffixes; (sz = strcspn(s, ", ")) != 0;) {
    309  1.37  christos 				addsuff(&suff_list, s, sz);
    310  1.37  christos 				s += sz;
    311  1.37  christos 				while (*s && strchr(", ", *s))
    312  1.37  christos 					s++;
    313  1.37  christos 			}
    314  1.37  christos 		} else
    315  1.37  christos 			addsuff(&suff_list, "", 0);
    316  1.22       dsl 	}
    317  1.22       dsl 
    318  1.18       dsl 	dependfile = open(filename, aflag, 0666);
    319  1.18       dsl 	if (dependfile == -1)
    320  1.42  christos 		goto wrerror;
    321  1.18       dsl 
    322  1.25       dsl 	while (dflag == 2 || *argv != NULL) {
    323  1.18       dsl 		if (dflag) {
    324  1.25       dsl 			if (dflag == 2) {
    325  1.25       dsl 				fname = read_fname();
    326  1.25       dsl 				if (fname == NULL)
    327  1.25       dsl 					break;
    328  1.25       dsl 			} else
    329  1.25       dsl 				fname = *argv++;
    330  1.42  christos 			if (iflag) {
    331  1.43  christos 				if (dprintf(dependfile, ".-include \"%s\"\n",
    332  1.42  christos 				    fname) < 0)
    333  1.42  christos 					goto wrerror;
    334  1.42  christos 				continue;
    335  1.42  christos 			}
    336  1.18       dsl 			fd = open(fname, O_RDONLY, 0);
    337  1.18       dsl 			if (fd == -1) {
    338  1.18       dsl 				if (!qflag)
    339  1.18       dsl 					warn("ignoring %s", fname);
    340  1.18       dsl 				continue;
    341  1.18       dsl 			}
    342  1.18       dsl 		} else {
    343  1.18       dsl 			fd = run_cc(argc, argv, &fname);
    344  1.18       dsl 			/* consume all args... */
    345  1.25       dsl 			argv += argc;
    346  1.18       dsl 		}
    347   1.1      tron 
    348  1.18       dsl 		sz = lseek(fd, 0, SEEK_END);
    349  1.18       dsl 		if (sz == 0) {
    350  1.18       dsl 			close(fd);
    351  1.18       dsl 			continue;
    352   1.1      tron 		}
    353  1.18       dsl 		buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
    354  1.18       dsl 		close(fd);
    355   1.1      tron 
    356  1.18       dsl 		if (buf == MAP_FAILED)
    357  1.25       dsl 			err(EXIT_FAILURE, "unable to mmap file %s", fname);
    358  1.20       dsl 		lim = buf + sz - 1;
    359  1.18       dsl 
    360  1.18       dsl 		/* Remove leading "./" from filenames */
    361  1.20       dsl 		for (ptr = buf; ptr < lim; ptr++) {
    362  1.20       dsl 			if (ptr[1] != '.' || ptr[2] != '/'
    363  1.20       dsl 			    || !isspace((unsigned char)ptr[0]))
    364  1.18       dsl 				continue;
    365  1.18       dsl 			ptr[1] = ' ';
    366  1.20       dsl 			ptr[2] = ' ';
    367   1.7       cgd 		}
    368   1.1      tron 
    369  1.20       dsl 		for (line = eol = buf; eol <= lim;) {
    370  1.20       dsl 			while (eol <= lim && *eol++ != '\n')
    371  1.22       dsl 				/* Find end of this line */
    372  1.20       dsl 				continue;
    373  1.19       dsl 			if (line == eol - 1) {
    374  1.18       dsl 				/* empty line - ignore */
    375  1.19       dsl 				line = eol;
    376  1.18       dsl 				continue;
    377  1.19       dsl 			}
    378  1.18       dsl 			if (eol[-2] == '\\')
    379  1.18       dsl 				/* Assemble continuation lines */
    380  1.18       dsl 				continue;
    381  1.20       dsl 			for (colon = line; *colon != ':'; colon++) {
    382  1.20       dsl 				if (colon >= eol) {
    383  1.20       dsl 					colon = NULL;
    384  1.20       dsl 					break;
    385  1.20       dsl 				}
    386  1.20       dsl 			}
    387  1.22       dsl 			if (isspace((unsigned char)*line) || colon == NULL) {
    388  1.21       dsl 				/* No dependency - just transcribe line */
    389  1.42  christos 				if (write(dependfile, line, eol - line) < 0)
    390  1.42  christos 					goto wrerror;
    391  1.21       dsl 				line = eol;
    392  1.21       dsl 				continue;
    393  1.21       dsl 			}
    394  1.22       dsl 			if (suff_list != NULL) {
    395  1.18       dsl 				/* Find the .o: */
    396  1.22       dsl 				/* First allow for any whitespace */
    397  1.29  christos 				for (suf = colon; suf > buf; suf--) {
    398  1.22       dsl 					if (!isspace((unsigned char)suf[-1]))
    399  1.22       dsl 						break;
    400  1.22       dsl 				}
    401  1.29  christos 				if (suf == buf)
    402  1.29  christos 					errx(EXIT_FAILURE,
    403  1.29  christos 					    "Corrupted file `%s'", fname);
    404  1.22       dsl 				/* Then look for any valid suffix */
    405  1.37  christos 				for (sl = suff_list; sl != NULL;
    406  1.37  christos 				    sl = sl->next) {
    407  1.38     enami 					if (sl->len && buf <= suf - sl->len &&
    408  1.37  christos 					    !memcmp(suf - sl->len, sl->suff,
    409  1.22       dsl 						    sl->len))
    410  1.18       dsl 						break;
    411  1.18       dsl 				}
    412  1.34  christos 				/*
    413  1.34  christos 				 * Not found, check for .o, since the
    414  1.34  christos 				 * original file will have it.
    415  1.34  christos 				 */
    416  1.37  christos 				if (sl == NULL) {
    417  1.34  christos 					if (memcmp(suf - 2, ".o", 2) == 0)
    418  1.34  christos 						slen = 2;
    419  1.34  christos 					else
    420  1.34  christos 						slen = 0;
    421  1.34  christos 				} else
    422  1.34  christos 					slen = sl->len;
    423  1.18       dsl 			}
    424  1.34  christos 			if (suff_list != NULL && slen != 0) {
    425  1.34  christos 				suf -= slen;
    426  1.37  christos 				for (sl = suff_list; sl != NULL; sl = sl->next)
    427  1.37  christos 				{
    428  1.22       dsl 					if (sl != suff_list)
    429  1.42  christos 						if (write(dependfile, " ", 1)
    430  1.42  christos 						    < 0)
    431  1.42  christos 							goto wrerror;
    432  1.41      jmmv 					if (prefix != NULL)
    433  1.42  christos 						if (write(dependfile, prefix,
    434  1.42  christos 						    strlen(prefix)) < 0)
    435  1.42  christos 							goto wrerror;
    436  1.42  christos 					if (write(dependfile, line,
    437  1.42  christos 					    suf - line) < 0)
    438  1.42  christos 						goto wrerror;
    439  1.42  christos 					if (write(dependfile, sl->suff,
    440  1.42  christos 					    sl->len) < 0)
    441  1.42  christos 						goto wrerror;
    442  1.18       dsl 				}
    443  1.42  christos 				if (write(dependfile, colon, eol - colon) < 0)
    444  1.42  christos 					goto wrerror;
    445  1.41      jmmv 			} else {
    446  1.41      jmmv 				if (prefix != NULL)
    447  1.42  christos 					if (write(dependfile, prefix,
    448  1.42  christos 					    strlen(prefix)) < 0)
    449  1.42  christos 						goto wrerror;
    450  1.42  christos 				if (write(dependfile, line, eol - line) < 0)
    451  1.42  christos 					goto wrerror;
    452  1.41      jmmv 			}
    453  1.18       dsl 
    454  1.21       dsl 			if (oflag)
    455  1.21       dsl 				save_for_optional(colon + 1, eol);
    456  1.19       dsl 			line = eol;
    457  1.18       dsl 		}
    458  1.18       dsl 		munmap(buf, sz);
    459   1.1      tron 	}
    460  1.21       dsl 
    461  1.21       dsl 	if (oflag && opt != NULL) {
    462  1.42  christos 		if (write(dependfile, ".OPTIONAL:", 10) < 0)
    463  1.42  christos 			goto wrerror;
    464  1.21       dsl 		width = 9;
    465  1.21       dsl 		sz = write_optional(dependfile, opt, 0);
    466  1.42  christos 		if (sz == (size_t)-1)
    467  1.42  christos 			goto wrerror;
    468  1.21       dsl 		/* 'depth' is about 39 for an i386 kernel */
    469  1.21       dsl 		/* fprintf(stderr, "Recursion depth %d\n", sz); */
    470  1.21       dsl 	}
    471  1.18       dsl 	close(dependfile);
    472   1.1      tron 
    473   1.7       cgd 	exit(EXIT_SUCCESS);
    474  1.42  christos wrerror:
    475  1.44  christos 	err(EXIT_FAILURE, "unable to %s to file %s",
    476  1.42  christos 	    aflag & O_TRUNC ? "write" : "append", filename);
    477  1.21       dsl }
    478  1.21       dsl 
    479  1.21       dsl 
    480  1.21       dsl /*
    481  1.21       dsl  * Only save each file once - the kernel .depend is 3MB and there is
    482  1.21       dsl  * no point doubling its size.
    483  1.21       dsl  * The data seems to be 'random enough' so the simple binary tree
    484  1.21       dsl  * only has a reasonable depth.
    485  1.21       dsl  */
    486  1.21       dsl static void
    487  1.21       dsl save_for_optional(const char *start, const char *limit)
    488  1.21       dsl {
    489  1.21       dsl 	opt_t **l, *n;
    490  1.21       dsl 	const char *name, *end;
    491  1.21       dsl 	int c;
    492  1.21       dsl 
    493  1.21       dsl 	while (start < limit && strchr(" \t\n\\", *start))
    494  1.21       dsl 		start++;
    495  1.21       dsl 	for (name = start; ; name = end) {
    496  1.21       dsl 		while (name < limit && strchr(" \t\n\\", *name))
    497  1.21       dsl 			name++;
    498  1.21       dsl 		for (end = name; end < limit && !strchr(" \t\n\\", *end);)
    499  1.21       dsl 			end++;
    500  1.21       dsl 		if (name >= limit)
    501  1.21       dsl 			break;
    502  1.21       dsl 		if (end[-1] == 'c' && end[-2] == '.' && name == start)
    503  1.21       dsl 			/* ignore dependency on the files own .c */
    504  1.21       dsl 			continue;
    505  1.21       dsl 		for (l = &opt;;) {
    506  1.21       dsl 			n = *l;
    507  1.21       dsl 			if (n == NULL) {
    508  1.21       dsl 				n = malloc(sizeof *n + (end - name));
    509  1.21       dsl 				n->left = n->right = 0;
    510  1.21       dsl 				n->len = end - name;
    511  1.21       dsl 				n->count = 1;
    512  1.21       dsl 				n->name[0] = ' ';
    513  1.21       dsl 				memcpy(n->name + 1, name, end - name);
    514  1.21       dsl 				*l = n;
    515  1.21       dsl 				break;
    516  1.21       dsl 			}
    517  1.21       dsl 			c = (end - name) - n->len;
    518  1.21       dsl 			if (c == 0)
    519  1.21       dsl 				c = memcmp(n->name + 1, name, (end - name));
    520  1.21       dsl 			if (c == 0) {
    521  1.21       dsl 				/* Duplicate */
    522  1.21       dsl 				n->count++;
    523  1.21       dsl 				break;
    524  1.21       dsl 			}
    525  1.21       dsl 			if (c < 0)
    526  1.21       dsl 				l = &n->left;
    527  1.21       dsl 			else
    528  1.21       dsl 				l = &n->right;
    529  1.21       dsl 		}
    530  1.21       dsl 	}
    531  1.21       dsl }
    532  1.21       dsl 
    533  1.42  christos static size_t
    534  1.42  christos write_optional(int fd, opt_t *node, size_t depth)
    535  1.21       dsl {
    536  1.42  christos 	size_t d1 = ++depth;
    537  1.21       dsl 
    538  1.21       dsl 	if (node->left)
    539  1.21       dsl 		d1 = write_optional(fd, node->left, d1);
    540  1.21       dsl 	if (width > 76 - node->len) {
    541  1.42  christos 		if (write(fd, " \\\n ", 4) < 0)
    542  1.42  christos 			return (size_t)-1;
    543  1.21       dsl 		width = 1;
    544  1.21       dsl 	}
    545  1.21       dsl 	width += 1 + node->len;
    546  1.42  christos 	if (write(fd, node->name, 1 + node->len) < 0)
    547  1.42  christos 		return (size_t)-1;
    548  1.21       dsl 	if (node->right)
    549  1.21       dsl 		depth = write_optional(fd, node->right, depth);
    550  1.21       dsl 	return d1 > depth ? d1 : depth;
    551   1.1      tron }
    552