/***************
*
* g:\exe\txf\src\txfmain.c
*/
#define MAIN 1
#include "txf.h"

#define MAX_ARGV	32
char far *fargv[MAX_ARGV];

void setdefault()
{
/*
	if (right > 0) right --;
	if (maxc > 0) maxc --;
*/
	if (cent > 0) {
		if (left < 0) left = (maxc - cent) / 2;
		if (right < 0) right = (maxc + cent) / 2;
	}
	if (maxc > 2000) {
		maxc = 2000;
		if (viewmode > 1) {
			fprintf(stderr,"Warning:Too long right limit. limit is resetting 2000.\n");
		}
	}
	if (left < 0) left = 0;
	if ((right < 0) || (right > maxc)) right = maxc - 4;

	if (viewmode > 2)
		fprintf(stderr,
		"\x1b[31mWarning:This is debug mode for nobody. (mode=%d)\x1b[0m\n",
		viewmode);

	if (tfile[0][0] == NUL) gettmpfile();
	mktfilename();

	if (kl == NULL) {
		kl = calloc(1, 1);
	}
	if (kf == NULL) {
		kf = calloc(1, 1);
		
	}
	if (kq == NULL) {
		kq = calloc(1, 1);
	}
	if ((kl == NULL) || (kf == NULL) || (kq == NULL)) {
		errexit("Heap Error at(setdefault)");
	}
}

void init(char *name)
{
	char *path;

#if !TXFL
	path = ((strlen(name) > 23) ? jstrrchr(name, '\\')+1 : name);
	fprintf(stderr,"%s Ver2.03b -original- copyright (C) 1992-1994 T.Nakatani\n", path);
#else
	path = ((strlen(name) > 26) ? jstrrchr(name,'\\')+1 : name);
	fprintf(stderr,"%s Ver2.03(compact) copyright (c) 1992-1994 T.Nakatani\n", path);
#endif

	init_allswitch();
	basedir=name;
	kl = NULL;
	kf = NULL;
	kq = NULL;
#if !TXFL
	log_idstr[0]  = NUL;
	log_resp[0]   = NUL;
	log_handle[0] = NUL;
	log_fname[0]  = NUL;
	log_pname[0]  = NUL;
	log_pname[8]  = NUL;
#endif
}

void txf()
{
	tmpinfile =- 1;
	if (removeeofflg) removeeof();
#if !TXFL
	if (exflg && (useoldex == 0)) exchangedriver();
	if (exflg && useoldex) old_exchangedriver();
#else
	if (exflg) exchangedriver();
#endif

	if (format) {
		if (base == 0) setleft0();	/*		*/
		if (base > 0) setleft();
		txform();
	}

	input_to_output();
}

void endoftxf()
{
	remove(tfile[0]);
	remove(tfile[1]);

	if (*outputfile == NUL) {
		fprintf(stderr, "\n");
	}
}

int main(int argc, char **argv)
{
	int i;
	init(argv[0]);
	command_option = 1;
	if (argc > MAX_ARGV) {
		errexit("too many command line arguments.");
	}
	else {
		for (i = 0; i < argc - 1; i++) {
			fargv[i] = argv[i+1];
		}
		fargv[i] = NULL;
	}
	swchk(argc - 1, fargv);
	command_option = 0;
	if (read_def_flg) read_def(argv[0]);
	setdefault();

/*
#if !NOHELP
		showswitch();
#endif
*/

	while (wcchk()) txf();

	endoftxf();
	return (0);
}

