2006-03-21 02:13:51 +03:00
|
|
|
/* $NetBSD: mkdep.c,v 1.28 2006/03/20 23:13:51 christos Exp $ */
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Matthias Scheler.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2001-03-22 05:41:02 +03:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
1999-01-26 01:10:42 +03:00
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-10-27 03:12:41 +03:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-10-27 03:12:41 +03:00
|
|
|
#if !defined(lint)
|
1999-01-26 01:10:42 +03:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
|
|
|
|
All rights reserved.\n");
|
2006-03-21 02:13:51 +03:00
|
|
|
__RCSID("$NetBSD: mkdep.c,v 1.28 2006/03/20 23:13:51 christos Exp $");
|
1999-01-26 01:10:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
#include <sys/mman.h>
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#include <err.h>
|
2003-11-10 20:56:38 +03:00
|
|
|
#include <fcntl.h>
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <locale.h>
|
|
|
|
#include <paths.h>
|
2003-01-16 11:05:09 +03:00
|
|
|
#include <signal.h>
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2002-06-15 03:14:18 +04:00
|
|
|
#include "findcc.h"
|
|
|
|
|
2003-12-07 23:22:49 +03:00
|
|
|
typedef struct opt opt_t;
|
|
|
|
struct opt {
|
|
|
|
opt_t *left;
|
|
|
|
opt_t *right;
|
|
|
|
int len;
|
|
|
|
int count;
|
|
|
|
char name[4];
|
|
|
|
};
|
|
|
|
|
2004-01-27 00:47:04 +03:00
|
|
|
typedef struct {
|
|
|
|
int len;
|
|
|
|
char suff[12];
|
|
|
|
} suff_list_t;
|
|
|
|
|
2003-12-07 23:22:49 +03:00
|
|
|
/* tree of includes for -o processing */
|
|
|
|
opt_t *opt;
|
|
|
|
int width;
|
|
|
|
|
1999-01-26 01:10:42 +03:00
|
|
|
#define DEFAULT_PATH _PATH_DEFPATH
|
|
|
|
#define DEFAULT_FILENAME ".depend"
|
|
|
|
|
2003-12-07 23:22:49 +03:00
|
|
|
static void save_for_optional(const char *, const char *);
|
|
|
|
static int write_optional(int, opt_t *, int);
|
|
|
|
|
2003-01-16 11:05:09 +03:00
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
static inline void *
|
|
|
|
deconst(const void *p)
|
|
|
|
{
|
|
|
|
return (const char *)p - (const char *)0 + (char *)0;
|
|
|
|
}
|
1999-01-26 01:10:42 +03:00
|
|
|
|
1999-03-31 15:26:45 +04:00
|
|
|
static void
|
2003-11-10 20:56:38 +03:00
|
|
|
usage(void)
|
1999-01-26 01:10:42 +03:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2005-12-13 01:49:37 +03:00
|
|
|
"usage: %s [-aDdopq] [-f file] [-s suffixes] -- [flags] file ...\n",
|
2001-03-22 03:16:50 +03:00
|
|
|
getprogname());
|
1999-01-26 01:10:42 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
static int
|
|
|
|
run_cc(int argc, char **argv, const char **fname)
|
2003-01-16 11:05:09 +03:00
|
|
|
{
|
2006-03-21 02:13:51 +03:00
|
|
|
const char *CC, *tmpdir;
|
|
|
|
char *pathname;
|
2003-11-10 20:56:38 +03:00
|
|
|
static char tmpfilename[MAXPATHLEN];
|
|
|
|
char **args;
|
|
|
|
int tmpfd;
|
|
|
|
pid_t pid, cpid;
|
|
|
|
int status;
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
if ((CC = getenv("CC")) == NULL)
|
|
|
|
CC = DEFAULT_CC;
|
|
|
|
if ((pathname = findcc(CC)) == NULL)
|
|
|
|
if (!setenv("PATH", DEFAULT_PATH, 1))
|
|
|
|
pathname = findcc(CC);
|
2003-11-10 20:56:38 +03:00
|
|
|
if (pathname == NULL)
|
|
|
|
err(EXIT_FAILURE, "%s: not found", CC);
|
|
|
|
if ((args = malloc((argc + 3) * sizeof(char *))) == NULL)
|
|
|
|
err(EXIT_FAILURE, "malloc");
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
args[0] = deconst(CC);
|
|
|
|
args[1] = deconst("-M");
|
1999-01-26 01:10:42 +03:00
|
|
|
(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
|
|
|
|
|
1999-07-21 19:20:55 +04:00
|
|
|
if ((tmpdir = getenv("TMPDIR")) == NULL)
|
|
|
|
tmpdir = _PATH_TMP;
|
|
|
|
(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
|
|
|
|
"mkdepXXXXXX");
|
2003-11-10 20:56:38 +03:00
|
|
|
if ((tmpfd = mkstemp(tmpfilename)) < 0) {
|
1999-01-26 01:10:42 +03:00
|
|
|
warn("unable to create temporary file %s", tmpfilename);
|
2001-03-22 05:33:47 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
2003-11-10 20:56:38 +03:00
|
|
|
(void)unlink(tmpfilename);
|
|
|
|
*fname = tmpfilename;
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
switch (cpid = vfork()) {
|
|
|
|
case 0:
|
2001-03-22 05:33:47 +03:00
|
|
|
(void)dup2(tmpfd, STDOUT_FILENO);
|
|
|
|
(void)close(tmpfd);
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2001-03-22 05:33:47 +03:00
|
|
|
(void)execv(pathname, args);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
/* NOTREACHED */
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
case -1:
|
2003-11-10 20:56:38 +03:00
|
|
|
err(EXIT_FAILURE, "unable to fork");
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
2006-03-20 20:18:06 +03:00
|
|
|
free(pathname);
|
|
|
|
free(args);
|
|
|
|
|
2001-03-22 05:33:47 +03:00
|
|
|
while (((pid = wait(&status)) != cpid) && (pid >= 0))
|
|
|
|
continue;
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
if (status)
|
|
|
|
errx(EXIT_FAILURE, "compile failed.");
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
return tmpfd;
|
|
|
|
}
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2005-12-13 01:05:28 +03:00
|
|
|
static const char *
|
|
|
|
read_fname(void)
|
|
|
|
{
|
|
|
|
static char *fbuf;
|
|
|
|
static int fbuflen;
|
|
|
|
int len, ch;
|
|
|
|
|
|
|
|
for (len = 0; (ch = getchar()) != EOF; len++) {
|
|
|
|
if (isspace(ch)) {
|
|
|
|
if (len != 0)
|
|
|
|
break;
|
|
|
|
len--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (len >= fbuflen - 1) {
|
|
|
|
fbuf = realloc(fbuf, fbuflen += 32);
|
|
|
|
if (fbuf == NULL)
|
|
|
|
err(EXIT_FAILURE, "no memory");
|
|
|
|
}
|
|
|
|
fbuf[len] = ch;
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
return NULL;
|
|
|
|
fbuf[len] = 0;
|
|
|
|
return fbuf;
|
|
|
|
}
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int aflag, dflag, oflag, qflag;
|
|
|
|
const char *filename;
|
|
|
|
int dependfile;
|
2003-11-11 13:55:24 +03:00
|
|
|
char *buf, *lim, *ptr, *line, *suf, *colon, *eol;
|
2003-11-10 20:56:38 +03:00
|
|
|
int ok_ind, ch;
|
|
|
|
int sz;
|
|
|
|
int fd;
|
|
|
|
const char *fname;
|
2004-01-27 00:47:04 +03:00
|
|
|
const char *suffixes = NULL, *s;
|
|
|
|
suff_list_t *suff_list = NULL, *sl;
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2005-06-07 13:33:37 +04:00
|
|
|
suf = NULL; /* XXXGCC -Wuninitialized [sun2] */
|
|
|
|
sl = NULL; /* XXXGCC -Wuninitialized [sun2] */
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
|
|
|
aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC;
|
|
|
|
dflag = 0;
|
|
|
|
oflag = 0;
|
|
|
|
qflag = 0;
|
|
|
|
filename = DEFAULT_FILENAME;
|
|
|
|
dependfile = -1;
|
|
|
|
|
|
|
|
opterr = 0; /* stop getopt() bleating about errors. */
|
2003-11-11 13:55:24 +03:00
|
|
|
for (;;) {
|
|
|
|
ok_ind = optind;
|
2005-12-13 01:49:37 +03:00
|
|
|
ch = getopt(argc, argv, "aDdf:opqs:");
|
2003-11-10 20:56:38 +03:00
|
|
|
switch (ch) {
|
2003-11-11 13:55:24 +03:00
|
|
|
case -1:
|
|
|
|
ok_ind = optind;
|
|
|
|
break;
|
2003-11-10 20:56:38 +03:00
|
|
|
case 'a': /* Append to output file */
|
|
|
|
aflag &= ~O_TRUNC;
|
|
|
|
continue;
|
2005-12-13 01:05:28 +03:00
|
|
|
case 'D': /* Process *.d files (don't run cc -M) */
|
|
|
|
dflag = 2; /* Read names from stdin */
|
|
|
|
opterr = 1;
|
|
|
|
continue;
|
2003-11-10 20:56:38 +03:00
|
|
|
case 'd': /* Process *.d files (don't run cc -M) */
|
|
|
|
dflag = 1;
|
|
|
|
opterr = 1;
|
|
|
|
continue;
|
|
|
|
case 'f': /* Name of output file */
|
|
|
|
filename = optarg;
|
|
|
|
continue;
|
|
|
|
case 'o': /* Mark dependant files .OPTIONAL */
|
|
|
|
oflag = 1;
|
|
|
|
continue;
|
|
|
|
case 'p': /* Program mode (x.o: -> x:) */
|
|
|
|
suffixes = "";
|
|
|
|
continue;
|
|
|
|
case 'q': /* Quiet */
|
|
|
|
qflag = 1;
|
|
|
|
continue;
|
|
|
|
case 's': /* Suffix list */
|
|
|
|
suffixes = optarg;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
if (dflag)
|
|
|
|
usage();
|
|
|
|
/* Unknown arguments are passed to "${CC} -M" */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
argc -= ok_ind;
|
|
|
|
argv += ok_ind;
|
2005-12-13 01:05:28 +03:00
|
|
|
if ((argc == 0 && !dflag) || (argc != 0 && dflag == 2))
|
2003-11-10 20:56:38 +03:00
|
|
|
usage();
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2004-01-27 00:47:04 +03:00
|
|
|
if (suffixes != NULL) {
|
|
|
|
/* parse list once and save names and lengths */
|
|
|
|
/* allocate an extra entry to mark end of list */
|
|
|
|
for (sz = 1, s = suffixes; *s != 0; s++)
|
|
|
|
if (*s == '.')
|
|
|
|
sz++;
|
|
|
|
suff_list = calloc(sz, sizeof *suff_list);
|
|
|
|
if (suff_list == NULL)
|
|
|
|
err(2, "malloc");
|
|
|
|
sl = suff_list;
|
|
|
|
for (s = suffixes; (s = strchr(s, '.')); s += sz, sl++ ) {
|
|
|
|
sz = strcspn(s, ", ");
|
|
|
|
if (sz > sizeof sl->suff)
|
|
|
|
errx(2, "suffix too long");
|
|
|
|
sl->len = sz;
|
|
|
|
memcpy(sl->suff, s, sz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
dependfile = open(filename, aflag, 0666);
|
|
|
|
if (dependfile == -1)
|
|
|
|
err(EXIT_FAILURE, "unable to %s to file %s\n",
|
|
|
|
aflag & O_TRUNC ? "write" : "append", filename);
|
|
|
|
|
2005-12-13 01:05:28 +03:00
|
|
|
while (dflag == 2 || *argv != NULL) {
|
2003-11-10 20:56:38 +03:00
|
|
|
if (dflag) {
|
2005-12-13 01:05:28 +03:00
|
|
|
if (dflag == 2) {
|
|
|
|
fname = read_fname();
|
|
|
|
if (fname == NULL)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
fname = *argv++;
|
2003-11-10 20:56:38 +03:00
|
|
|
fd = open(fname, O_RDONLY, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!qflag)
|
|
|
|
warn("ignoring %s", fname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd = run_cc(argc, argv, &fname);
|
|
|
|
/* consume all args... */
|
2005-12-13 01:05:28 +03:00
|
|
|
argv += argc;
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
2003-11-10 20:56:38 +03:00
|
|
|
sz = lseek(fd, 0, SEEK_END);
|
|
|
|
if (sz == 0) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (buf == MAP_FAILED)
|
2005-12-13 01:05:28 +03:00
|
|
|
err(EXIT_FAILURE, "unable to mmap file %s", fname);
|
2003-11-11 13:55:24 +03:00
|
|
|
lim = buf + sz - 1;
|
2003-11-10 20:56:38 +03:00
|
|
|
|
|
|
|
/* Remove leading "./" from filenames */
|
2003-11-11 13:55:24 +03:00
|
|
|
for (ptr = buf; ptr < lim; ptr++) {
|
|
|
|
if (ptr[1] != '.' || ptr[2] != '/'
|
|
|
|
|| !isspace((unsigned char)ptr[0]))
|
2003-11-10 20:56:38 +03:00
|
|
|
continue;
|
|
|
|
ptr[1] = ' ';
|
2003-11-11 13:55:24 +03:00
|
|
|
ptr[2] = ' ';
|
2001-03-22 05:33:47 +03:00
|
|
|
}
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2003-11-11 13:55:24 +03:00
|
|
|
for (line = eol = buf; eol <= lim;) {
|
|
|
|
while (eol <= lim && *eol++ != '\n')
|
2004-01-27 00:47:04 +03:00
|
|
|
/* Find end of this line */
|
2003-11-11 13:55:24 +03:00
|
|
|
continue;
|
2003-11-10 21:43:15 +03:00
|
|
|
if (line == eol - 1) {
|
2003-11-10 20:56:38 +03:00
|
|
|
/* empty line - ignore */
|
2003-11-10 21:43:15 +03:00
|
|
|
line = eol;
|
2003-11-10 20:56:38 +03:00
|
|
|
continue;
|
2003-11-10 21:43:15 +03:00
|
|
|
}
|
2003-11-10 20:56:38 +03:00
|
|
|
if (eol[-2] == '\\')
|
|
|
|
/* Assemble continuation lines */
|
|
|
|
continue;
|
2003-11-11 13:55:24 +03:00
|
|
|
for (colon = line; *colon != ':'; colon++) {
|
|
|
|
if (colon >= eol) {
|
|
|
|
colon = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-01-27 00:47:04 +03:00
|
|
|
if (isspace((unsigned char)*line) || colon == NULL) {
|
2003-12-07 23:22:49 +03:00
|
|
|
/* No dependency - just transcribe line */
|
|
|
|
write(dependfile, line, eol - line);
|
|
|
|
line = eol;
|
|
|
|
continue;
|
|
|
|
}
|
2004-01-27 00:47:04 +03:00
|
|
|
if (suff_list != NULL) {
|
2003-11-10 20:56:38 +03:00
|
|
|
/* Find the .o: */
|
2004-01-27 00:47:04 +03:00
|
|
|
/* First allow for any whitespace */
|
|
|
|
for (suf = colon; ; suf--) {
|
|
|
|
if (!isspace((unsigned char)suf[-1]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Then look for any valid suffix */
|
|
|
|
for (sl = suff_list; sl->len != 0; sl++) {
|
|
|
|
if (!memcmp(suf - sl->len, sl->suff,
|
|
|
|
sl->len))
|
2003-11-10 20:56:38 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-01-27 00:47:04 +03:00
|
|
|
if (suff_list != NULL && sl->len != 0) {
|
|
|
|
suf -= sl->len;
|
|
|
|
for (sl = suff_list; sl->len != 0; sl++) {
|
|
|
|
if (sl != suff_list)
|
|
|
|
write(dependfile, " ", 1);
|
2003-11-10 20:56:38 +03:00
|
|
|
write(dependfile, line, suf - line);
|
2004-01-27 00:47:04 +03:00
|
|
|
write(dependfile, sl->suff, sl->len);
|
2003-11-10 20:56:38 +03:00
|
|
|
}
|
|
|
|
write(dependfile, colon, eol - colon);
|
|
|
|
} else
|
|
|
|
write(dependfile, line, eol - line);
|
|
|
|
|
2003-12-07 23:22:49 +03:00
|
|
|
if (oflag)
|
|
|
|
save_for_optional(colon + 1, eol);
|
2003-11-10 21:43:15 +03:00
|
|
|
line = eol;
|
2003-11-10 20:56:38 +03:00
|
|
|
}
|
|
|
|
munmap(buf, sz);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
2003-12-07 23:22:49 +03:00
|
|
|
|
|
|
|
if (oflag && opt != NULL) {
|
|
|
|
write(dependfile, ".OPTIONAL:", 10);
|
|
|
|
width = 9;
|
|
|
|
sz = write_optional(dependfile, opt, 0);
|
|
|
|
/* 'depth' is about 39 for an i386 kernel */
|
|
|
|
/* fprintf(stderr, "Recursion depth %d\n", sz); */
|
|
|
|
}
|
2003-11-10 20:56:38 +03:00
|
|
|
close(dependfile);
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2001-03-22 05:33:47 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
2003-12-07 23:22:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only save each file once - the kernel .depend is 3MB and there is
|
|
|
|
* no point doubling its size.
|
|
|
|
* The data seems to be 'random enough' so the simple binary tree
|
|
|
|
* only has a reasonable depth.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
save_for_optional(const char *start, const char *limit)
|
|
|
|
{
|
|
|
|
opt_t **l, *n;
|
|
|
|
const char *name, *end;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while (start < limit && strchr(" \t\n\\", *start))
|
|
|
|
start++;
|
|
|
|
for (name = start; ; name = end) {
|
|
|
|
while (name < limit && strchr(" \t\n\\", *name))
|
|
|
|
name++;
|
|
|
|
for (end = name; end < limit && !strchr(" \t\n\\", *end);)
|
|
|
|
end++;
|
|
|
|
if (name >= limit)
|
|
|
|
break;
|
|
|
|
if (end[-1] == 'c' && end[-2] == '.' && name == start)
|
|
|
|
/* ignore dependency on the files own .c */
|
|
|
|
continue;
|
|
|
|
for (l = &opt;;) {
|
|
|
|
n = *l;
|
|
|
|
if (n == NULL) {
|
|
|
|
n = malloc(sizeof *n + (end - name));
|
|
|
|
n->left = n->right = 0;
|
|
|
|
n->len = end - name;
|
|
|
|
n->count = 1;
|
|
|
|
n->name[0] = ' ';
|
|
|
|
memcpy(n->name + 1, name, end - name);
|
|
|
|
*l = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = (end - name) - n->len;
|
|
|
|
if (c == 0)
|
|
|
|
c = memcmp(n->name + 1, name, (end - name));
|
|
|
|
if (c == 0) {
|
|
|
|
/* Duplicate */
|
|
|
|
n->count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c < 0)
|
|
|
|
l = &n->left;
|
|
|
|
else
|
|
|
|
l = &n->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_optional(int fd, opt_t *node, int depth)
|
|
|
|
{
|
|
|
|
int d1 = ++depth;
|
|
|
|
|
|
|
|
if (node->left)
|
|
|
|
d1 = write_optional(fd, node->left, d1);
|
|
|
|
if (width > 76 - node->len) {
|
|
|
|
write(fd, " \\\n ", 4);
|
|
|
|
width = 1;
|
|
|
|
}
|
|
|
|
width += 1 + node->len;
|
|
|
|
write(fd, node->name, 1 + node->len);
|
|
|
|
if (node->right)
|
|
|
|
depth = write_optional(fd, node->right, depth);
|
|
|
|
return d1 > depth ? d1 : depth;
|
|
|
|
}
|