/*
 *     Read in makefile
 */

#include <stdio.h>
#include	<ctype.h>
#include "h.h"

int			lineno;

/* DJ: this is the PAMAKE version of getline, which prints the
	make file name as well as the line # (for #include)

	Notes for future: This would actually be less kludgey with format().
	Also, 'stderr' has no meaning on Amiga - perhaps open "*"??
*/

/*
 *      Syntax error handler.  Print message, with line number, and exits.
 */
void
error(msg, a1, a2, a3)
char			*msg;
{	fprintf(stderr, "%s: ", myname);
	fprintf(stderr, msg, a1, a2, a3);
	if (lineno)
		fprintf(stderr, " in file %s on line %d", fname[nestlvl], lineno);
	fputc('\n', stderr);
    exit(1);
}

/* DJ: this is the PAMAKE version of getline. */

/*
 *      Read a line into the supplied string of length LZ.  Remove
 *      comments, ignore blank lines. Deal with quoted (\) #, and
 *      quoted newlines.  If EOF return TRUE.
 */

bool getline(str) char *str;
{   register char	*p;
	char			*q;
	char			*a;
	int			  	pos = 0;
	FILE			*fd;
	FILE			*incf;
	int		     	specialhash;

	if (nestlvl < 0) return TRUE;	   /* EOF */

	for (;;)
	{
		fd = ifile[nestlvl];					/* file handle in nesting array */
		if (fgets(str+pos, LZ-pos, fd) == (char *)0)	/* get a line */
		{		/* if we couldn't get a line */
			fclose(fd);							/* close file */
			if (nestlvl == 0) { nestlvl--; /* ifeof(); */ return TRUE; }	/* finish */
			fln[nestlvl] = 0;					/* init line number */
			nestlvl--;							/* dec nesting level */
			continue;							/* get another line */
		}
		lineno = ++fln[nestlvl];				/* bump line count */

		if ((p = strchr(str+pos, '\n')) == (char *)0)	/* look for cr */
			error("Input line is too long");

		if (p[-1] == '\\')
		{
/*			p[-1] = '\n'; */
			p--;						/* added by DJ for AmigaDOS compatability */
			pos = p - str;
			continue;
		}

		if (!strncmp(str,"#ifn",4)) specialhash = 2;
		else if (!strncmp(str,"#if",3)) specialhash = 1;
		else if (!strncmp(str,"#else",5)) specialhash = 3;
		else if (!strncmp(str,"#endif",6)) specialhash = 4;
		else if (!strncmp(str,"#include",8)) specialhash = 5;
		else specialhash = 0;

		p = str + (specialhash != 0);
		while (((q = strchr(p, '#')) != (char *)0) &&
		    (p != q) && (q[-1] == '\\'))
		{
			a = q - 1;      /*  Del \ chr; move rest back  */
			p = q;
			while (*a++ = *q++)
				;
		}

		if (q != (char *)0) 
		{
			while ( (q != str) && (isspace(q[-1])) ) q--;
			q[0] = '\n';
			q[1] = '\0';
		}

/*		if (ifproc(str,specialhash)) {pos = 0; continue;} */

		if (specialhash == 5)
		{
			q = str + 8;
			while (isspace(q[0])) q++;
			if (nestlvl >= 3)
			    fatal("Include files nested too deeply");
			a = q + strlen(q) - 1;
			if (*a == '\n') *a = '\0';
			expand(q);
			incf = fopen(q,"r");
			if (incf == (FILE *)0)
				fatal("Unable to open include file %s", q);
			ifile[++nestlvl] = incf;
			strncpy(fname[nestlvl],q,80);
			continue;
		}

		p = str;
		while (isspace(*p))     /*  Checking for blank  */
			p++;

		if (*p != '\0')  
			return FALSE;
		pos = 0;
	}
}

/*
 *     Get a word from the current line, surounded by white space.
 *     return a pointer to it. String returned has no white spaces
 *     in it.
 */
char *gettok(ptr)
	char			**ptr;
{	register char	*p;

	while (isspace(**ptr)) 			/*  Skip spaces	*/
		(*ptr)++;

	if (**ptr == '\0')				/*  Nothing after spaces  */
		return NULL;

	p = *ptr;						/*  word starts here  */

	while ((**ptr != '\0') && (!isspace(**ptr)))
		(*ptr)++;					/*  Find end of word  */

	*(*ptr)++ = '\0';				/*  Terminate it  */

	return(p);
}
