security/nss/coreconf/mkdepend/main.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/coreconf/mkdepend/main.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,870 @@
     1.4 +/* $Xorg: main.c,v 1.5 2001/02/09 02:03:16 xorgcvs Exp $ */
     1.5 +/*
     1.6 +
     1.7 +Copyright (c) 1993, 1994, 1998 The Open Group
     1.8 +
     1.9 +Permission to use, copy, modify, distribute, and sell this software and its
    1.10 +documentation for any purpose is hereby granted without fee, provided that
    1.11 +the above copyright notice appear in all copies and that both that
    1.12 +copyright notice and this permission notice appear in supporting
    1.13 +documentation.
    1.14 +
    1.15 +The above copyright notice and this permission notice shall be included in
    1.16 +all copies or substantial portions of the Software.
    1.17 +
    1.18 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.19 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.20 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    1.21 +THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    1.22 +AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    1.23 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.24 +
    1.25 +Except as contained in this notice, the name of The Open Group shall not be
    1.26 +used in advertising or otherwise to promote the sale, use or other dealings
    1.27 +in this Software without prior written authorization from The Open Group.
    1.28 +
    1.29 +*/
    1.30 +/* $XFree86: xc/config/makedepend/main.c,v 3.32 2003/03/26 20:43:48 tsi Exp $ */
    1.31 +
    1.32 +#include "def.h"
    1.33 +#ifdef hpux
    1.34 +#define sigvec sigvector
    1.35 +#endif /* hpux */
    1.36 +
    1.37 +#ifdef X_POSIX_C_SOURCE
    1.38 +#define _POSIX_C_SOURCE X_POSIX_C_SOURCE
    1.39 +#include <signal.h>
    1.40 +#undef _POSIX_C_SOURCE
    1.41 +#else
    1.42 +#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
    1.43 +#include <signal.h>
    1.44 +#else
    1.45 +#define _POSIX_SOURCE
    1.46 +#include <signal.h>
    1.47 +#undef _POSIX_SOURCE
    1.48 +#endif
    1.49 +#endif
    1.50 +
    1.51 +#include <stdarg.h>
    1.52 +#ifdef _WIN32
    1.53 +#include <io.h>
    1.54 +#endif
    1.55 +
    1.56 +#ifdef MINIX
    1.57 +#define USE_CHMOD	1
    1.58 +#endif
    1.59 +
    1.60 +#ifdef DEBUG
    1.61 +int	_debugmask;
    1.62 +#endif
    1.63 +
    1.64 +/* #define DEBUG_DUMP */
    1.65 +#ifdef DEBUG_DUMP
    1.66 +#define DBG_PRINT(file, fmt, args)   fprintf(file, fmt, args)
    1.67 +#else
    1.68 +#define DBG_PRINT(file, fmt, args)   /* empty */
    1.69 +#endif
    1.70 +
    1.71 +#define DASH_INC_PRE    "#include \""
    1.72 +#define DASH_INC_POST   "\""
    1.73 +
    1.74 +char *ProgramName;
    1.75 +
    1.76 +char	*directives[] = {
    1.77 +	"if",
    1.78 +	"ifdef",
    1.79 +	"ifndef",
    1.80 +	"else",
    1.81 +	"endif",
    1.82 +	"define",
    1.83 +	"undef",
    1.84 +	"include",
    1.85 +	"line",
    1.86 +	"pragma",
    1.87 +	"error",
    1.88 +	"ident",
    1.89 +	"sccs",
    1.90 +	"elif",
    1.91 +	"eject",
    1.92 +	"warning",
    1.93 +	"include_next",
    1.94 +	NULL
    1.95 +};
    1.96 +
    1.97 +#define MAKEDEPEND
    1.98 +#include "imakemdep.h"	/* from config sources */
    1.99 +#undef MAKEDEPEND
   1.100 +
   1.101 +struct	inclist inclist[ MAXFILES ],
   1.102 +		*inclistp = inclist,
   1.103 +		*inclistnext = inclist,
   1.104 +		maininclist;
   1.105 +
   1.106 +static char	*filelist[ MAXFILES ];
   1.107 +char		*includedirs[ MAXDIRS + 1 ],
   1.108 +		**includedirsnext = includedirs;
   1.109 +char		*notdotdot[ MAXDIRS ];
   1.110 +static int	cmdinc_count = 0;
   1.111 +static char	*cmdinc_list[ 2 * MAXINCFILES ];
   1.112 +char		*objprefix = "";
   1.113 +char		*objsuffix = OBJSUFFIX;
   1.114 +static char	*startat = "# DO NOT DELETE";
   1.115 +int		width = 78;
   1.116 +static boolean	append = FALSE;
   1.117 +boolean		printed = FALSE;
   1.118 +boolean		verbose = FALSE;
   1.119 +boolean		show_where_not = FALSE;
   1.120 +/* Warn on multiple includes of same file */
   1.121 +boolean 	warn_multiple = FALSE;
   1.122 +
   1.123 +static void setfile_cmdinc(struct filepointer *filep, long count, char **list);
   1.124 +static void redirect(char *line, char *makefile);
   1.125 +
   1.126 +static
   1.127 +#ifdef SIGNALRETURNSINT
   1.128 +int
   1.129 +#else
   1.130 +void
   1.131 +#endif
   1.132 +catch (int sig)
   1.133 +{
   1.134 +	fflush (stdout);
   1.135 +	fatalerr ("got signal %d\n", sig);
   1.136 +}
   1.137 +
   1.138 +#if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(__UNIXOS2__) || defined(Lynx_22) || defined(__CYGWIN__)
   1.139 +#define USGISH
   1.140 +#endif
   1.141 +
   1.142 +#ifndef USGISH
   1.143 +#ifdef X_NOT_POSIX
   1.144 +#define sigaction sigvec
   1.145 +#define sa_handler sv_handler
   1.146 +#define sa_mask sv_mask
   1.147 +#define sa_flags sv_flags
   1.148 +#endif
   1.149 +struct sigaction sig_act;
   1.150 +#endif /* USGISH */
   1.151 +
   1.152 +int
   1.153 +main(int argc, char *argv[])
   1.154 +{
   1.155 +	char	**fp = filelist;
   1.156 +	char	**incp = includedirs;
   1.157 +	char	*p;
   1.158 +	struct inclist	*ip;
   1.159 +	char	*makefile = NULL;
   1.160 +	struct filepointer	*filecontent;
   1.161 +	struct symtab *psymp = predefs;
   1.162 +	char *endmarker = NULL;
   1.163 +	char *defincdir = NULL;
   1.164 +	char **undeflist = NULL;
   1.165 +	int numundefs = 0, i;
   1.166 +	register char offset;
   1.167 +
   1.168 +	ProgramName = argv[0];
   1.169 +
   1.170 +	while (psymp->s_name)
   1.171 +	{
   1.172 +	    define2(psymp->s_name, psymp->s_value, &maininclist);
   1.173 +	    psymp++;
   1.174 +	}
   1.175 +	if (argc == 2 && argv[1][0] == '@') {
   1.176 +	    struct stat ast;
   1.177 +	    int afd;
   1.178 +	    char *args;
   1.179 +	    char **nargv;
   1.180 +	    int nargc;
   1.181 +	    char quotechar = '\0';
   1.182 +
   1.183 +	    nargc = 1;
   1.184 +	    if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
   1.185 +		fatalerr("cannot open \"%s\"\n", argv[1]+1);
   1.186 +	    fstat(afd, &ast);
   1.187 +	    args = (char *)malloc(ast.st_size + 1);
   1.188 +	    if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
   1.189 +		fatalerr("failed to read %s\n", argv[1]+1);
   1.190 +	    args[ast.st_size] = '\0';
   1.191 +	    close(afd);
   1.192 +	    for (p = args; *p; p++) {
   1.193 +		if (quotechar) {
   1.194 +		    if (quotechar == '\\' ||
   1.195 +			(*p == quotechar && p[-1] != '\\'))
   1.196 +			quotechar = '\0';
   1.197 +		    continue;
   1.198 +		}
   1.199 +		switch (*p) {
   1.200 +		case '\\':
   1.201 +		case '"':
   1.202 +		case '\'':
   1.203 +		    quotechar = *p;
   1.204 +		    break;
   1.205 +		case ' ':
   1.206 +		case '\n':
   1.207 +		    *p = '\0';
   1.208 +		    if (p > args && p[-1])
   1.209 +			nargc++;
   1.210 +		    break;
   1.211 +		}
   1.212 +	    }
   1.213 +	    if (p[-1])
   1.214 +		nargc++;
   1.215 +	    nargv = (char **)malloc(nargc * sizeof(char *));
   1.216 +	    nargv[0] = argv[0];
   1.217 +	    argc = 1;
   1.218 +	    for (p = args; argc < nargc; p += strlen(p) + 1)
   1.219 +		if (*p) nargv[argc++] = p;
   1.220 +	    argv = nargv;
   1.221 +	}
   1.222 +	for(argc--, argv++; argc; argc--, argv++) {
   1.223 +	    	/* if looking for endmarker then check before parsing */
   1.224 +		if (endmarker && strcmp (endmarker, *argv) == 0) {
   1.225 +		    endmarker = NULL;
   1.226 +		    continue;
   1.227 +		}
   1.228 +		if (**argv != '-') {
   1.229 +			/* treat +thing as an option for C++ */
   1.230 +			if (endmarker && **argv == '+')
   1.231 +				continue;
   1.232 +			*fp++ = argv[0];
   1.233 +			continue;
   1.234 +		}
   1.235 +		switch(argv[0][1]) {
   1.236 +		case '-':
   1.237 +			endmarker = &argv[0][2];
   1.238 +			if (endmarker[0] == '\0') endmarker = "--";
   1.239 +			break;
   1.240 +		case 'D':
   1.241 +			offset = 2;
   1.242 +			if (argv[0][2] == '\0') {
   1.243 +				argv++;
   1.244 +				argc--;
   1.245 +				offset = 0;
   1.246 +			}
   1.247 +			/* offset +1 here since first def letter
   1.248 +			 * cannot be `=`
   1.249 +			 */
   1.250 +			for (p = argv[0] + offset + 1; *p; p++)
   1.251 +				if (*p == '=') {
   1.252 +					*p = ' ';
   1.253 +					break;
   1.254 +				}
   1.255 +			define(argv[0] + offset, &maininclist);
   1.256 +			break;
   1.257 +		case 'I':
   1.258 +			if (incp >= includedirs + MAXDIRS)
   1.259 +			    fatalerr("Too many -I flags.\n");
   1.260 +			*incp++ = argv[0]+2;
   1.261 +			if (**(incp-1) == '\0') {
   1.262 +				*(incp-1) = *(++argv);
   1.263 +				argc--;
   1.264 +			}
   1.265 +			break;
   1.266 +		case 'U':
   1.267 +			/* Undef's override all -D's so save them up */
   1.268 +			numundefs++;
   1.269 +			if (numundefs == 1)
   1.270 +			    undeflist = malloc(sizeof(char *));
   1.271 +			else
   1.272 +			    undeflist = realloc(undeflist,
   1.273 +						numundefs * sizeof(char *));
   1.274 +			offset = 2;
   1.275 +			if (argv[0][2] == '\0') {
   1.276 +				argv++;
   1.277 +				argc--;
   1.278 +				offset = 0;
   1.279 +			}
   1.280 +			undeflist[numundefs - 1] = argv[0] + offset;
   1.281 +			break;
   1.282 +		case 'Y':
   1.283 +			defincdir = argv[0]+2;
   1.284 +			break;
   1.285 +		/* do not use if endmarker processing */
   1.286 +		case 'a':
   1.287 +			if (endmarker) break;
   1.288 +			append = TRUE;
   1.289 +			break;
   1.290 +		case 'w':
   1.291 +			if (endmarker) break;
   1.292 +			if (argv[0][2] == '\0') {
   1.293 +				argv++;
   1.294 +				argc--;
   1.295 +				width = atoi(argv[0]);
   1.296 +			} else
   1.297 +				width = atoi(argv[0]+2);
   1.298 +			break;
   1.299 +		case 'o':
   1.300 +			if (endmarker) break;
   1.301 +			if (argv[0][2] == '\0') {
   1.302 +				argv++;
   1.303 +				argc--;
   1.304 +				objsuffix = argv[0];
   1.305 +			} else
   1.306 +				objsuffix = argv[0]+2;
   1.307 +			break;
   1.308 +		case 'p':
   1.309 +			if (endmarker) break;
   1.310 +			if (argv[0][2] == '\0') {
   1.311 +				argv++;
   1.312 +				argc--;
   1.313 +				objprefix = argv[0];
   1.314 +			} else
   1.315 +				objprefix = argv[0]+2;
   1.316 +			break;
   1.317 +		case 'v':
   1.318 +			if (endmarker) break;
   1.319 +			verbose = TRUE;
   1.320 +#ifdef DEBUG
   1.321 +			if (argv[0][2])
   1.322 +				_debugmask = atoi(argv[0]+2);
   1.323 +#endif
   1.324 +			break;
   1.325 +		case 's':
   1.326 +			if (endmarker) break;
   1.327 +			startat = argv[0]+2;
   1.328 +			if (*startat == '\0') {
   1.329 +				startat = *(++argv);
   1.330 +				argc--;
   1.331 +			}
   1.332 +			if (*startat != '#')
   1.333 +				fatalerr("-s flag's value should start %s\n",
   1.334 +					"with '#'.");
   1.335 +			break;
   1.336 +		case 'f':
   1.337 +			if (endmarker) break;
   1.338 +			makefile = argv[0]+2;
   1.339 +			if (*makefile == '\0') {
   1.340 +				makefile = *(++argv);
   1.341 +				argc--;
   1.342 +			}
   1.343 +			break;
   1.344 +
   1.345 +		case 'm':
   1.346 +			warn_multiple = TRUE;
   1.347 +			break;
   1.348 +			
   1.349 +		/* Ignore -O, -g so we can just pass ${CFLAGS} to
   1.350 +		   makedepend
   1.351 +		 */
   1.352 +		case 'O':
   1.353 +		case 'g':
   1.354 +			break;
   1.355 +		case 'i':
   1.356 +			if (strcmp(&argv[0][1],"include") == 0) {
   1.357 +				char *buf;
   1.358 +				if (argc<2)
   1.359 +					fatalerr("option -include is a "
   1.360 +						 "missing its parameter\n");
   1.361 +				if (cmdinc_count >= MAXINCFILES)
   1.362 +					fatalerr("Too many -include flags.\n");
   1.363 +				argc--;
   1.364 +				argv++;
   1.365 +				buf = malloc(strlen(DASH_INC_PRE) +
   1.366 +					     strlen(argv[0]) +
   1.367 +					     strlen(DASH_INC_POST) + 1);
   1.368 +                		if(!buf)
   1.369 +					fatalerr("out of memory at "
   1.370 +						 "-include string\n");
   1.371 +				cmdinc_list[2 * cmdinc_count + 0] = argv[0];
   1.372 +				cmdinc_list[2 * cmdinc_count + 1] = buf;
   1.373 +				cmdinc_count++;
   1.374 +				break;
   1.375 +			}
   1.376 +			/* intentional fall through */
   1.377 +		default:
   1.378 +			if (endmarker) break;
   1.379 +	/*		fatalerr("unknown opt = %s\n", argv[0]); */
   1.380 +			warning("ignoring option %s\n", argv[0]);
   1.381 +		}
   1.382 +	}
   1.383 +	/* Now do the undefs from the command line */
   1.384 +	for (i = 0; i < numundefs; i++)
   1.385 +	    undefine(undeflist[i], &maininclist);
   1.386 +	if (numundefs > 0)
   1.387 +	    free(undeflist);
   1.388 +
   1.389 +	if (!defincdir) {
   1.390 +#ifdef PREINCDIR
   1.391 +	    if (incp >= includedirs + MAXDIRS)
   1.392 +		fatalerr("Too many -I flags.\n");
   1.393 +	    *incp++ = PREINCDIR;
   1.394 +#endif
   1.395 +#if defined(__UNIXOS2__) || defined(_MSC_VER)
   1.396 +	    {
   1.397 +#if defined(_MSC_VER)
   1.398 +		char *includepath = getenv("INCLUDE");
   1.399 +#else
   1.400 +		char *includepath = getenv("C_INCLUDE_PATH");
   1.401 +#endif
   1.402 +		/* can have more than one component */
   1.403 +		if (includepath) {
   1.404 +		    char *beg, *end;
   1.405 +		    beg= (char*)strdup(includepath);
   1.406 +		    for (;;) {
   1.407 +			end = (char*)strchr(beg,';');
   1.408 +			if (end) *end = 0;
   1.409 +		    	if (incp >= includedirs + MAXDIRS)
   1.410 +				fatalerr("Too many include dirs\n");
   1.411 +			*incp++ = beg;
   1.412 +			if (!end) break;
   1.413 +			beg = end+1;
   1.414 +		    }
   1.415 +		}
   1.416 +	    }
   1.417 +#else /* !__UNIXOS2__ && !_MSC_VER, does not use INCLUDEDIR at all */
   1.418 +	    if (incp >= includedirs + MAXDIRS)
   1.419 +		fatalerr("Too many -I flags.\n");
   1.420 +	    *incp++ = INCLUDEDIR;
   1.421 +#endif
   1.422 +
   1.423 +#ifdef EXTRAINCDIR
   1.424 +	    if (incp >= includedirs + MAXDIRS)
   1.425 +		fatalerr("Too many -I flags.\n");
   1.426 +	    *incp++ = EXTRAINCDIR;
   1.427 +#endif
   1.428 +
   1.429 +#ifdef POSTINCDIR
   1.430 +	    if (incp >= includedirs + MAXDIRS)
   1.431 +		fatalerr("Too many -I flags.\n");
   1.432 +	    *incp++ = POSTINCDIR;
   1.433 +#endif
   1.434 +	} else if (*defincdir) {
   1.435 +	    if (incp >= includedirs + MAXDIRS)
   1.436 +		fatalerr("Too many -I flags.\n");
   1.437 +	    *incp++ = defincdir;
   1.438 +	}
   1.439 +
   1.440 +	redirect(startat, makefile);
   1.441 +
   1.442 +	/*
   1.443 +	 * catch signals.
   1.444 +	 */
   1.445 +#ifdef USGISH
   1.446 +/*  should really reset SIGINT to SIG_IGN if it was.  */
   1.447 +#ifdef SIGHUP
   1.448 +	signal (SIGHUP, catch);
   1.449 +#endif
   1.450 +	signal (SIGINT, catch);
   1.451 +#ifdef SIGQUIT
   1.452 +	signal (SIGQUIT, catch);
   1.453 +#endif
   1.454 +	signal (SIGILL, catch);
   1.455 +#ifdef SIGBUS
   1.456 +	signal (SIGBUS, catch);
   1.457 +#endif
   1.458 +	signal (SIGSEGV, catch);
   1.459 +#ifdef SIGSYS
   1.460 +	signal (SIGSYS, catch);
   1.461 +#endif
   1.462 +#else
   1.463 +	sig_act.sa_handler = catch;
   1.464 +#if defined(_POSIX_SOURCE) || !defined(X_NOT_POSIX)
   1.465 +	sigemptyset(&sig_act.sa_mask);
   1.466 +	sigaddset(&sig_act.sa_mask, SIGINT);
   1.467 +	sigaddset(&sig_act.sa_mask, SIGQUIT);
   1.468 +#ifdef SIGBUS
   1.469 +	sigaddset(&sig_act.sa_mask, SIGBUS);
   1.470 +#endif
   1.471 +	sigaddset(&sig_act.sa_mask, SIGILL);
   1.472 +	sigaddset(&sig_act.sa_mask, SIGSEGV);
   1.473 +	sigaddset(&sig_act.sa_mask, SIGHUP);
   1.474 +	sigaddset(&sig_act.sa_mask, SIGPIPE);
   1.475 +#ifdef SIGSYS
   1.476 +	sigaddset(&sig_act.sa_mask, SIGSYS);
   1.477 +#endif
   1.478 +#else
   1.479 +	sig_act.sa_mask = ((1<<(SIGINT -1))
   1.480 +			   |(1<<(SIGQUIT-1))
   1.481 +#ifdef SIGBUS
   1.482 +			   |(1<<(SIGBUS-1))
   1.483 +#endif
   1.484 +			   |(1<<(SIGILL-1))
   1.485 +			   |(1<<(SIGSEGV-1))
   1.486 +			   |(1<<(SIGHUP-1))
   1.487 +			   |(1<<(SIGPIPE-1))
   1.488 +#ifdef SIGSYS
   1.489 +			   |(1<<(SIGSYS-1))
   1.490 +#endif
   1.491 +			   );
   1.492 +#endif /* _POSIX_SOURCE */
   1.493 +	sig_act.sa_flags = 0;
   1.494 +	sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
   1.495 +	sigaction(SIGINT, &sig_act, (struct sigaction *)0);
   1.496 +	sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
   1.497 +	sigaction(SIGILL, &sig_act, (struct sigaction *)0);
   1.498 +#ifdef SIGBUS
   1.499 +	sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
   1.500 +#endif
   1.501 +	sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
   1.502 +#ifdef SIGSYS
   1.503 +	sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
   1.504 +#endif
   1.505 +#endif /* USGISH */
   1.506 +
   1.507 +	/*
   1.508 +	 * now peruse through the list of files.
   1.509 +	 */
   1.510 +	for(fp=filelist; *fp; fp++) {
   1.511 +		DBG_PRINT(stderr,"file: %s\n",*fp);
   1.512 +		filecontent = getfile(*fp);
   1.513 +		setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list);
   1.514 +		ip = newinclude(*fp, (char *)NULL);
   1.515 +
   1.516 +		find_includes(filecontent, ip, ip, 0, FALSE);
   1.517 +		freefile(filecontent);
   1.518 +		recursive_pr_include(ip, ip->i_file, base_name(*fp));
   1.519 +		inc_clean();
   1.520 +	}
   1.521 +	if (printed)
   1.522 +		printf("\n");
   1.523 +	return 0;
   1.524 +}
   1.525 +
   1.526 +#ifdef __UNIXOS2__
   1.527 +/*
   1.528 + * eliminate \r chars from file
   1.529 + */
   1.530 +static int 
   1.531 +elim_cr(char *buf, int sz)
   1.532 +{
   1.533 +	int i,wp;
   1.534 +	for (i= wp = 0; i<sz; i++) {
   1.535 +		if (buf[i] != '\r')
   1.536 +			buf[wp++] = buf[i];
   1.537 +	}
   1.538 +	return wp;
   1.539 +}
   1.540 +#endif
   1.541 +
   1.542 +struct filepointer *
   1.543 +getfile(char *file)
   1.544 +{
   1.545 +	int	fd;
   1.546 +	struct filepointer	*content;
   1.547 +	struct stat	st;
   1.548 +
   1.549 +	content = (struct filepointer *)malloc(sizeof(struct filepointer));
   1.550 +	content->f_name = file;
   1.551 +	if ((fd = open(file, O_RDONLY)) < 0) {
   1.552 +		warning("cannot open \"%s\"\n", file);
   1.553 +		content->f_p = content->f_base = content->f_end = (char *)malloc(1);
   1.554 +		*content->f_p = '\0';
   1.555 +		return(content);
   1.556 +	}
   1.557 +	fstat(fd, &st);
   1.558 +	content->f_base = (char *)malloc(st.st_size+1);
   1.559 +	if (content->f_base == NULL)
   1.560 +		fatalerr("cannot allocate mem\n");
   1.561 +	if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
   1.562 +		fatalerr("failed to read %s\n", file);
   1.563 +#ifdef __UNIXOS2__
   1.564 +	st.st_size = elim_cr(content->f_base,st.st_size);
   1.565 +#endif
   1.566 +	close(fd);
   1.567 +	content->f_len = st.st_size+1;
   1.568 +	content->f_p = content->f_base;
   1.569 +	content->f_end = content->f_base + st.st_size;
   1.570 +	*content->f_end = '\0';
   1.571 +	content->f_line = 0;
   1.572 +	content->cmdinc_count = 0;
   1.573 +	content->cmdinc_list = NULL;
   1.574 +	content->cmdinc_line = 0;
   1.575 +	return(content);
   1.576 +}
   1.577 +
   1.578 +void
   1.579 +setfile_cmdinc(struct filepointer* filep, long count, char** list)
   1.580 +{
   1.581 +	filep->cmdinc_count = count;
   1.582 +	filep->cmdinc_list = list;
   1.583 +	filep->cmdinc_line = 0;
   1.584 +}
   1.585 +
   1.586 +void
   1.587 +freefile(struct filepointer *fp)
   1.588 +{
   1.589 +	free(fp->f_base);
   1.590 +	free(fp);
   1.591 +}
   1.592 +
   1.593 +char *copy(char *str)
   1.594 +{
   1.595 +	char	*p = (char *)malloc(strlen(str) + 1);
   1.596 +
   1.597 +	strcpy(p, str);
   1.598 +	return(p);
   1.599 +}
   1.600 +
   1.601 +int
   1.602 +match(char *str, char **list)
   1.603 +{
   1.604 +	int	i;
   1.605 +
   1.606 +	for (i=0; *list; i++, list++)
   1.607 +		if (strcmp(str, *list) == 0)
   1.608 +			return(i);
   1.609 +	return(-1);
   1.610 +}
   1.611 +
   1.612 +/*
   1.613 + * Get the next line.  We only return lines beginning with '#' since that
   1.614 + * is all this program is ever interested in.
   1.615 + */
   1.616 +char *getnextline(struct filepointer *filep)
   1.617 +{
   1.618 +	char	*p,	/* walking pointer */
   1.619 +		*eof,	/* end of file pointer */
   1.620 +		*bol;	/* beginning of line pointer */
   1.621 +	int	lineno;	/* line number */
   1.622 +	boolean whitespace = FALSE;
   1.623 +
   1.624 +	/*
   1.625 +	 * Fake the "-include" line files in form of #include to the
   1.626 +	 * start of each file.
   1.627 +	 */
   1.628 +	if (filep->cmdinc_line < filep->cmdinc_count) {
   1.629 +		char *inc = filep->cmdinc_list[2 * filep->cmdinc_line + 0];
   1.630 +		char *buf = filep->cmdinc_list[2 * filep->cmdinc_line + 1];
   1.631 +		filep->cmdinc_line++;
   1.632 +		sprintf(buf,"%s%s%s",DASH_INC_PRE,inc,DASH_INC_POST);
   1.633 +		DBG_PRINT(stderr,"%s\n",buf);
   1.634 +		return(buf);
   1.635 +	}
   1.636 +
   1.637 +	p = filep->f_p;
   1.638 +	eof = filep->f_end;
   1.639 +	if (p >= eof)
   1.640 +		return((char *)NULL);
   1.641 +	lineno = filep->f_line;
   1.642 +
   1.643 +	for (bol = p--; ++p < eof; ) {
   1.644 +		if ((bol == p) && ((*p == ' ') || (*p == '\t')))
   1.645 +		{
   1.646 +			/* Consume leading white-spaces for this line */
   1.647 +			while (((p+1) < eof) && ((*p == ' ') || (*p == '\t')))
   1.648 +			{
   1.649 +				p++;
   1.650 +				bol++;
   1.651 +			}
   1.652 +			whitespace = TRUE;
   1.653 +		}
   1.654 +        
   1.655 +		if (*p == '/' && (p+1) < eof && *(p+1) == '*') {
   1.656 +			/* Consume C comments */
   1.657 +			*(p++) = ' ';
   1.658 +			*(p++) = ' ';
   1.659 +			while (p < eof && *p) {
   1.660 +				if (*p == '*' && (p+1) < eof && *(p+1) == '/') {
   1.661 +					*(p++) = ' ';
   1.662 +					*(p++) = ' ';
   1.663 +					break;
   1.664 +				}
   1.665 +				if (*p == '\n')
   1.666 +					lineno++;
   1.667 +				*(p++) = ' ';
   1.668 +			}
   1.669 +			--p;
   1.670 +		}
   1.671 +		else if (*p == '/' && (p+1) < eof && *(p+1) == '/') {
   1.672 +			/* Consume C++ comments */
   1.673 +			*(p++) = ' ';
   1.674 +			*(p++) = ' ';
   1.675 +			while (p < eof && *p) {
   1.676 +				if (*p == '\\' && (p+1) < eof &&
   1.677 +				    *(p+1) == '\n') {
   1.678 +					*(p++) = ' ';
   1.679 +					lineno++;
   1.680 +				}
   1.681 +				else if (*p == '?' && (p+3) < eof &&
   1.682 +					 *(p+1) == '?' && 
   1.683 +					 *(p+2) == '/' &&
   1.684 +					 *(p+3) == '\n') {
   1.685 +					*(p++) = ' ';
   1.686 +					*(p++) = ' ';
   1.687 +					*(p++) = ' ';
   1.688 +					lineno++;
   1.689 +				}
   1.690 +				else if (*p == '\n')
   1.691 +					break;	/* to process end of line */
   1.692 +				*(p++) = ' ';
   1.693 +			}
   1.694 +			--p;
   1.695 +		}
   1.696 +		else if (*p == '\\' && (p+1) < eof && *(p+1) == '\n') {
   1.697 +			/* Consume backslash line terminations */
   1.698 +			*(p++) = ' ';
   1.699 +			*p = ' ';
   1.700 +			lineno++;
   1.701 +		}
   1.702 +		else if (*p == '?' && (p+3) < eof &&
   1.703 +			 *(p+1) == '?' && *(p+2) == '/' && *(p+3) == '\n') {
   1.704 +			/* Consume trigraph'ed backslash line terminations */
   1.705 +			*(p++) = ' ';
   1.706 +			*(p++) = ' ';
   1.707 +			*(p++) = ' ';
   1.708 +			*p = ' ';
   1.709 +			lineno++;
   1.710 +		}
   1.711 +		else if (*p == '\n') {
   1.712 +			lineno++;
   1.713 +			if (*bol == '#') {
   1.714 +				char *cp;
   1.715 +
   1.716 +				*(p++) = '\0';
   1.717 +				/* punt lines with just # (yacc generated) */
   1.718 +				for (cp = bol+1; 
   1.719 +				     *cp && (*cp == ' ' || *cp == '\t'); cp++);
   1.720 +				if (*cp) goto done;
   1.721 +				--p;
   1.722 +			}
   1.723 +			bol = p+1;
   1.724 +			whitespace = FALSE;
   1.725 +		}
   1.726 +	}
   1.727 +	if (*bol != '#')
   1.728 +		bol = NULL;
   1.729 +done:
   1.730 +#if !defined(__UNIXOS2__) && !defined(_MSC_VER) && !defined(_WIN32)
   1.731 +	/* Don't print warnings for system header files */
   1.732 +	if (bol && whitespace && !strstr(filep->f_name, INCLUDEDIR)) {
   1.733 +		warning("%s:  non-portable whitespace encountered at line %d\n",
   1.734 +			filep->f_name, lineno);
   1.735 +	}
   1.736 +#endif
   1.737 +	filep->f_p = p;
   1.738 +	filep->f_line = lineno;
   1.739 +#ifdef DEBUG_DUMP
   1.740 +	if (bol)
   1.741 +		DBG_PRINT(stderr,"%s\n",bol);
   1.742 +#endif
   1.743 +	return(bol);
   1.744 +}
   1.745 +
   1.746 +/*
   1.747 + * Strip the file name down to what we want to see in the Makefile.
   1.748 + * It will have objprefix and objsuffix around it.
   1.749 + */
   1.750 +char *base_name(char *file)
   1.751 +{
   1.752 +	char	*p;
   1.753 +
   1.754 +	file = copy(file);
   1.755 +	for(p=file+strlen(file); p>file && *p != '.'; p--) ;
   1.756 +
   1.757 +	if (*p == '.')
   1.758 +		*p = '\0';
   1.759 +	return(file);
   1.760 +}
   1.761 +
   1.762 +#if defined(USG) && !defined(CRAY) && !defined(SVR4) && !defined(__UNIXOS2__) && !defined(clipper) && !defined(__clipper__)
   1.763 +int rename (char *from, char *to)
   1.764 +{
   1.765 +    (void) unlink (to);
   1.766 +    if (link (from, to) == 0) {
   1.767 +	unlink (from);
   1.768 +	return 0;
   1.769 +    } else {
   1.770 +	return -1;
   1.771 +    }
   1.772 +}
   1.773 +#endif /* USGISH */
   1.774 +
   1.775 +void
   1.776 +redirect(char *line, char *makefile)
   1.777 +{
   1.778 +	struct stat	st;
   1.779 +	FILE	*fdin, *fdout;
   1.780 +	char	backup[ BUFSIZ ],
   1.781 +		buf[ BUFSIZ ];
   1.782 +	boolean	found = FALSE;
   1.783 +	int	len;
   1.784 +
   1.785 +	/*
   1.786 +	 * if makefile is "-" then let it pour onto stdout.
   1.787 +	 */
   1.788 +	if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
   1.789 +		puts(line);
   1.790 +		return;
   1.791 +	}
   1.792 +
   1.793 +	/*
   1.794 +	 * use a default makefile is not specified.
   1.795 +	 */
   1.796 +	if (!makefile) {
   1.797 +		if (stat("Makefile", &st) == 0)
   1.798 +			makefile = "Makefile";
   1.799 +		else if (stat("makefile", &st) == 0)
   1.800 +			makefile = "makefile";
   1.801 +		else
   1.802 +			fatalerr("[mM]akefile is not present\n");
   1.803 +	}
   1.804 +	else
   1.805 +	    stat(makefile, &st);
   1.806 +	if ((fdin = fopen(makefile, "r")) == NULL)
   1.807 +		fatalerr("cannot open \"%s\"\n", makefile);
   1.808 +	sprintf(backup, "%s.bak", makefile);
   1.809 +	unlink(backup);
   1.810 +#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
   1.811 +	fclose(fdin);
   1.812 +#endif
   1.813 +	if (rename(makefile, backup) < 0)
   1.814 +		fatalerr("cannot rename %s to %s\n", makefile, backup);
   1.815 +#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
   1.816 +	if ((fdin = fopen(backup, "r")) == NULL)
   1.817 +		fatalerr("cannot open \"%s\"\n", backup);
   1.818 +#endif
   1.819 +	if ((fdout = freopen(makefile, "w", stdout)) == NULL)
   1.820 +		fatalerr("cannot open \"%s\"\n", backup);
   1.821 +	len = strlen(line);
   1.822 +	while (!found && fgets(buf, BUFSIZ, fdin)) {
   1.823 +		if (*buf == '#' && strncmp(line, buf, len) == 0)
   1.824 +			found = TRUE;
   1.825 +		fputs(buf, fdout);
   1.826 +	}
   1.827 +	if (!found) {
   1.828 +		if (verbose)
   1.829 +		warning("Adding new delimiting line \"%s\" and dependencies...\n",
   1.830 +			line);
   1.831 +		puts(line); /* same as fputs(fdout); but with newline */
   1.832 +	} else if (append) {
   1.833 +	    while (fgets(buf, BUFSIZ, fdin)) {
   1.834 +		fputs(buf, fdout);
   1.835 +	    }
   1.836 +	}
   1.837 +	fflush(fdout);
   1.838 +#if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
   1.839 +	chmod(makefile, st.st_mode);
   1.840 +#else
   1.841 +        fchmod(fileno(fdout), st.st_mode);
   1.842 +#endif /* USGISH */
   1.843 +}
   1.844 +
   1.845 +void
   1.846 +fatalerr(char *msg, ...)
   1.847 +{
   1.848 +	va_list args;
   1.849 +	fprintf(stderr, "%s: error:  ", ProgramName);
   1.850 +	va_start(args, msg);
   1.851 +	vfprintf(stderr, msg, args);
   1.852 +	va_end(args);
   1.853 +	exit (1);
   1.854 +}
   1.855 +
   1.856 +void
   1.857 +warning(char *msg, ...)
   1.858 +{
   1.859 +	va_list args;
   1.860 +	fprintf(stderr, "%s: warning:  ", ProgramName);
   1.861 +	va_start(args, msg);
   1.862 +	vfprintf(stderr, msg, args);
   1.863 +	va_end(args);
   1.864 +}
   1.865 +
   1.866 +void
   1.867 +warning1(char *msg, ...)
   1.868 +{
   1.869 +	va_list args;
   1.870 +	va_start(args, msg);
   1.871 +	vfprintf(stderr, msg, args);
   1.872 +	va_end(args);
   1.873 +}

mercurial