/*====================================================================
 *
 * TXF command file module
 *
 *====================================================================
 *                                   copyright(C) 1992-1994 T.Nakatani
 *====================================================================
 */
#include "txf.h"

void read_def(char *own)
{
	struct find_t filedata;
	char *period, buf[80], *txfdef;

	if ((txfdef = getenv("TXFDEF")) != NULL) {
		strncpy(buf, txfdef, 80);
		if (_dos_findfirst(buf,
				_A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) != 0) {
			if ((strchr(buf, '\0')-1) != (jstrrchr(buf,'\\')))
				strcat(buf, "\\");
			strcat(buf, "TXF.DEF");
		}
	}
	else {
		strcpy(buf, own);
		period=strchr(buf, '.');
		strcpy(period, ".DEF");
	}
	if (_dos_findfirst(buf, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) == 0)
		read_commandfile(buf, 0);

/*
		fprintf(stderr,"Special:DEF FILE:%s\n",buf);
*/
}

void savemem(char chr)
{

	*(fbuf+fbufptr) = chr;
	fbufptr++;
	if (fbufptr >= (fbufsize * 1024)) {
		fbufsize++;
		fbuf = farrealloc(fbuf, fbufsize*1024);
		if (fbuf == NULL) {
			fprintf(stderr, "Error:Heap Error at commandfile(%d)",
				fbufsize*1024);
			errexit("Too long command / DEF parameters.");
		}
/*		if (fbufsize > 3) {
			rbufsize = 16384;
		}
*/
/*
		printf("fbuf=%Nd,fbufptr=%d,fbufsize=%d, chr=0x%X, next=0x%X\n",fbuf, fbufptr,fbufsize, chr,fbuf[fbufptr+1]);
*/
	}
}

void read_commandfile(char *commandfile, int level)
{
	FILE *fr;
	int paraptr=0, chr=0;
	char *ptr,*txfpath;
	char filename[80];
	struct find_t filedata;
	static int fbufbase=0;

	if (((txfpath = getenv("TXFPATH")) != NULL) && level) {
/*		fprintf(stderr, "0:%s\n", filename);	*/
		if ((*commandfile != '\\') && (*(commandfile+1) != ':')) {
			strncpy(filename, txfpath, 80);
			if ((strchr(filename, '\0') - 1) != (jstrrchr(filename, '\\'))) {
				strcat(filename, "\\");
			}
		}
		else {
			*filename = NUL;
		}
		strcat(filename, commandfile);
	}
	else {
		strcpy(filename, commandfile);
	}
/*	fprintf(stderr, "1:%s\n", filename);	*/

	ptr = jstrrchr(filename, '\\');
	if (ptr == NULL) ptr = filename;
	if (strchr(ptr, '.') == NULL) strcat(filename, ".TXF");
	if (_dos_findfirst(filename,
			_A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) != 0) {
		strcpy(filename, commandfile);
		if (strchr(ptr, '.') == NULL) strcat(filename, ".TXF");
/*		fprintf(stderr, "2:%s\n", filename);	*/
	}
/*	fprintf(stderr, "3:%s\n", filename);	*/

	fbufbase = fbufptr;
	if (_dos_findfirst(filename,
			_A_ARCH | _A_NORMAL | _A_RDONLY, &filedata) == 0) {

		fr = fopen(filename, "r");
		if (fr == NULL) {
			fprintf(stderr, "Info:filename=<%s>\n", filename);
			errexit("cannot open DEF/command file\n");
		}
		if (fbufsize == 0) {
			fbuf = farmalloc(4096);
			if (fbuf == NULL) {
				errexit("Heap Error at(read DEFfile)");
			}
			fbufsize = 4;
		}
		while (chr != EOF) {
			chr = getc(fr);
			if (chr == EOF) continue;
			if (chr == sep) {
				chr = getc(fr);
				while ((chr != EOF) && (chr != sep)) {
					savemem(chr);
					chr=getc(fr);
				}
				savemem(NUL);
			}
			else if (chr == '#') {
				while ((chr != EOF) && (chr != '\n')) chr = getc(fr);
			}
			else if (chr > ' ') {
				paraptr = fbufptr;
				while ((chr != EOF) && (chr > ' ')) {
					savemem(chr);
					chr = getc(fr);
				}
				savemem(NUL);
				if ((*(fbuf+paraptr) == '-') && ((*(fbuf+paraptr+1) == 's') ||
						(*(fbuf+paraptr+1) == 'S'))) {
					while ((chr != EOF) && (chr <= ' ')) chr = getc(fr);
					if (chr != EOF) sep = chr;
					savemem(chr);
					savemem(NUL);
				}
			}
		}
		savesw(fbufbase);
	}
	else {
		if (viewmode > 0) {
			fprintf(stderr,
				"Warning:commandfile'%s' is not found...\n", filename);
		}
	}
}

void savesw(int base)
{
	static int i = 0;
	int j;
	char far *ptr;
/*	char *env;	*/

	while ((i > 0) && (*(para + i-1) == NULL)) {
		i--;
	}
	j = i;
	ptr = (fbuf + base);
	while ((ptr - fbuf) < fbufptr) {
		if (*ptr == NUL) {
			ptr++;
		}
		else {
			*(para + i) = ptr;
			i += 1;
			if (i >= PARA_MAX) {
				errexit("too many parameters");
			}
			ptr = far_strchr(ptr, NUL);
		}
	}
	*(para + i) = NULL;

/*
	if ((env = getenv("TXF")) != NULL) {
		if (strcmp(env,"TEST") == 0) {
			for (k = 0; k < i; k++) {
				fprintf(stderr, "Info:PARA[%d]=<%s>\n", k, nrcpy(*(para+k)));
			}
			fprintf(stderr, "Info:(i,j)=(%d,%d)\n", i, j);
		}
	}
*/

	swchk(i - j, para + j);

}

