/*
 *  MBTERM.C - 6/20/87 - Terminal emulators and things.
 */

/*
 *  Modifiy History - by A.Yamada(JA3JWI)
 *
 *  2. Allow append open for log file. T[A-KM-Y] [>>] log_file     AY-871203
 *  1. Use Line Edite in Terminal mode
 */
#include "mb.h"

char *talkm1, *talkm2, *talkm3, *talkm4;

/*
 *  User wants to talk to the console.
 */

utalk()
{
	register PORTS *p;
	register short i;

	p = port;
	prtx(talkm2); 
	ioport(cport); 
	prtx(talkm4);

	for (i = 0; i < 10; i++)
	{
#if	(XENIX|UNIX|BSD)
		mbsleep(2);
		if (s_param & s_page) outchar(ctl_g);
		mbsleep(2);
#else
		wait(2);
		if (s_param & s_page) outchar(ctl_g);
		wait(2);
#endif

		if (s_param & s_page) outchar(ctl_g);

		ioport(cport);
		if (instat()) { 
                        term(cport, p, false);  /* add False */
			ioport(p); 
			return; 
		}

		ioport(p);
		if (instat()) return;
		ioport(cport);
	}
	ioport(p); 
	port->msg = talkm3;
}

/*
 *  Console used as terminal to tnc.
 */


extern char crwas[20], cpwas[20], sewas[20];
resume_tnc()
{
	port->flags clrbit p_clrsys;

/*	cmdtnc(); */
	onetnc(crwas);
	onetnc(cpwas);
	onetnc(sewas);
/*	convtnc(); */
}

save_tnc_mode()
{
	port->flags setbit p_clrsys;

	if (port->dev is p_tnc) {
/*		cmdtnc(); */
		port->flags clrbit p_give;
		outstr("cr on\n");
		getdat();
		pparse();
		sprintf(crwas, "%s %s\n", port->fld[0], port->fld[2]);
		waitcmd();
		outstr("cp off\n");
		getdat();
		pparse();
		sprintf(cpwas, "%s %s\n", port->fld[0], port->fld[2]);
		waitcmd();
		outstr("se $0d\n");
		getdat();
		pparse();
		sprintf(sewas, "%s %s\n", port->fld[0], port->fld[2]);
		waitcmd();
		port->flags setbit p_give;
	}
}


static char *cantopen = "MB: Can't open out-file\n";

lterm()
{
register PORTS *m, *s;

	m = port;

	if (m->flds is 2)
	if ((m->fl = fopen(m->fld[1], "r")) isnt NULL) { 
		fclose(m->fl); 
		m->msg = mexst; 
		return; 
	}

	if ((s = findport(m->opt2)) is NULL) { 
		m->msg = mnport; 
		return; 
	}
	if (s is m) { 
		m->msg = mcant;  
		return; 
	}
	if (!(s->mode & idle)) { 
		m->msg = minuse; 
		return; 
	}

	ioport(s);
	switch(s->dev) {
	case p_memory:
		if (!allocport()) { 
			ioport(m); 
			m->msg = minuse; 
			return; 
		}
	case p_hayes:
	case p_serial:
		prtx("*** CONNECTED to $O\n");
		break;

	default: 
		;
	}

	m->fl = NULL;		/* AY-881031 */
	if (m->flds is 2) {
		m->fl = fopen(m->fld[1], "w");
		if (m->fl is NULL) { 
			m->msg = cantopen; 
			return; 
		}
	}

#if (XENIX|UNIX|BSD)                   /* CHANGED SMC */
        save_tnc_mode();               /* AY-881031 */
#endif
        term(m, s, (m->fl isnt NULL));  /* add true/false */
#if (XENIX|UNIX|BSD)                   /* CHANGED SMC */
        ioport(s);                     /* AY-881031 */
        resume_tnc();                  /* AY-881031 */
#endif
        ioport(m);
	if (m->fl isnt NULL) fclose(m->fl);
}

/*
 *  General "do terminal" things.
 */

term(m, s, flg)
PORTS *m, *s; short flg ;  /* add flg NOV11,89 */
{
	register char ch;

	while(true)
	{
		ioport(m);
		if (instat())
		{
			if ((ch = inchar()) is '\r') ch = '\n';
			if (m->dev is p_console)
			{
				if (ch is rchar) return;
				ioport(s); 
				outchar(ch);
			}
                   /*     if (m->fl isnt NULL) if (ch isnt cpmeof) putc(ch, m->fl); */
                          if (flg is true) if (ch isnt cpmeof) putc (ch, m->fl) ;
		}

		ioport(s);
		if (instat())
		{
			if ((ch = inchar()) is '\r') ch = '\n';
			if (s->dev is p_console)
			{
				if (ch is rchar) return;
				ioport(m); 
				outchar(ch);
			}
                  /*      if (m->fl isnt NULL) if (ch isnt cpmeof) putc(ch, m->fl); */
                        if (flg is true) if (ch isnt cpmeof) putc (ch, m->fl) ;
		}
	}
}

otalk()
{
	register char ch;
	PORTS *p;

	p = port;
	ioport(cport);
	if (!instat()) { 
		ioport(p); 
		return 0; 
	}

	ch = inchar();
	ioport(p);

	if (ch is tchar) {
		cmdtnc();		/* TNC to Command mode first */
		save_tnc_mode();	/* Set the TNC to Terminal mode */
		convtnc();		/* Resume the TNC to convers mode */
		prtx(talkm1);
		ioport(cport);
		prtx(mumsg);
                term(cport, p, false);  /* NOV.11,89 */
		ioport(p);
		tncstate();
		resume_tnc();		/* Resume the TNC Parameter's */
		convtnc();
		if (port->mode & (discon | forced)) return 2;
		return 1;
	}

	if (ch is achar)
	{
		port->mode = forced;
		return 2;
	}

	return 0;
}
