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