/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: depend.c 1.4 89/10/25 $"

#include <stdio.h>
#include <ctype.h>
#include "config.h"
#include <sys/stat.h>

extern	char	*malloc();
extern	char	*realloc();

static	char	*buff;
static	int	cnt;
static	int	bsize;
#define	LL	56

/*
 * if (Dflag == 1) {
 *  grep the header dependancies for #includes.
 *  (This is somewhat crude and "cpp -[MH]" is better).
 * }
 */

char *
depend(file , init)
	char	*file;
	char	*init;
{
	char		read_buff[BUFSIZ];
	register char	*p;
	register int	lcnt;
	register int	old_cnt;
	char		c;
	FILE		*fp;
	int		old_lcnt;

	if (DEBUG_LEVEL(12))
		printf("Depend(\"%s\",\"%s\")\n",file,init);

	if (Dflag == 0)
		return(init);
	/*
	 * open the file.
	 */
	if ((fp=fopen(file,"r")) == NULL) {
		perror("mkdepend");
		fatal("cannot open %s\n",file);
		exit(1);
	}
	cnt = 0;
	lcnt = 20;
	/*
	 * Reuse the buffer ?
	 */
	if (bsize == 0) {
		bsize = BUFSIZ;
		if ((buff = malloc(bsize)) == (char *)NULL) {
			fatal("depend: unable to allocate space\n");
			exit(1);
		}
	}
	/*
	 * Prepend the initial string.
	 */
	p = init;
	while(*p) {
		buff[cnt++] = *p++;
		if (cnt >= bsize)
			strext();
		if (++lcnt > LL) {
			/*
			 *  The code below reads: "< 4" because later on, the
			 *  code assumes that you do *not* need to check for
			 *  space *before* placing a character in the buffer.
			 */
			if ((bsize - cnt) < 4)
				strext();
			buff[cnt++] = '\\';
			buff[cnt++] = '\n';
			buff[cnt++] = '\t';
			lcnt = 8;
		}
	}
	/*
	 * Read each line search for #include
	 */
	while(fgets(read_buff, BUFSIZ, fp) != NULL) {
		if (read_buff[0] != '#')
			continue;
		p = &read_buff[1];
		c = *p;
		while (isspace(c))
			c = *++p;
		if (strncmp("include", p, 7) != 0)
			continue;
		/*
		 * Matches. Adjust line length and extract the file name.
		 */
		/*
		 * span forward.
		 */
		p += 8;
		c = *p;
		while (isspace(c))
			c = *++p;
		/*
		 * calculate the delimiter.
		 */
		switch(c = *p++){
		case '<':
			c = '>';
			/* fall through */
		case '"':
			break;
		default:
			continue;
		}
		/*
		 * adjust line length
		 */
		old_cnt = cnt;
		old_lcnt = lcnt;
		if (lcnt > LL) {
			/*
			 *  The code below reads: "< 4" because later on, the
			 *  code assumes that you do *not* need to check for
			 *  space *before* placing a character in the buffer.
			 */
			if ((bsize - cnt) < 4)
				strext();
			buff[cnt++] = '\\';
			buff[cnt++] = '\n';
			buff[cnt++] = '\t';
			lcnt = 8;
		}
		/*
		 * prepend include path.
		 */
		if (bsize-cnt < 9) 
			strext();
		strcpy(&buff[cnt], " $(INC)/");
		cnt += 8;
		while(*p && (*p != c)) {
			buff[cnt++] = *p++;
			if (cnt >= bsize)
				strext();
		}
		if (*p == '\0') {		/* Delimiter not found */
			cnt = old_cnt;
			lcnt = old_lcnt;
		} else
			lcnt += (cnt-old_cnt);
	}
	buff[cnt++] = '\0';
	fclose(fp);
	return(buff);
}


/*
 * extend the current string using realloc().
 */
strext()
{
	bsize += BUFSIZ;
	if ((buff=realloc(buff, bsize)) == (char *)NULL){
		fatal("depend: unable to allocate space");
		exit(1);
	}
}
