/*	TERM2.C		Subroutines of TERM.C, Part 2
			Setup of communication parameters
	14 Feb 1988	A.Herrmann
*/

#include "stdio.h"
#include "term.h"
#include "term.e"


void setup()	/* Sets new communication parameters		*/
		/* On ABORT char, calls finis() to exit to CP/M	*/
{
	static int no, res;

	dissta();			/* disable status line update	*/
	selwdw(STAWDW);
	for (no=0; no < MAXITEM; no++) {
		if ((res = item(no)) == QUIT || res==ABORT) break;
		if (res == PRV) {
			if (no)	no -= 2;
			else	break;
		}
	}
	selwdw(TERWDW);
	if (res==ABORT) finis();	
}


static char buffer[15];		/* file name input buffer	*/


item(no)	/* gets new communication parameter number no		*/
int no;		/* Returns code for previous, next item, quit or exit	*/
{
	static int ioitem;	/* TRUE iff infile of outfile item	*/
	static int res,ind;	/* response, index			*/

	ioitem = (no==IFITEM || no==OFITEM);
	do {
		res = (ioitem) ? gtitem(no,buffer) : gtitch(no);
		switch (res) {
		case ABORT:	return ABORT;
		case PRV:
		case _BS:	return PRV;
		case QUIT:
		case SWITCH:
		case _ESC:	return QUIT;
		case NXT:
			if (ioitem)			/* Input,output file */
				(*par[no].fct)(buffer);	/* fall through	     */
		case _CR:	return NXT;
		case _BLANK:
			ind = par[no].ind		/* next alternative  */
 			    = (par[no].ind+1) % par[no].nra;
			cpitem(par[no].alt[ind],no);	/* copy value	     */
			(*par[no].fct)(ind);		/* change settings   */
			ptitem(no);			/* display new value */
			break;
		}
	} while (TRUE);
}


gtitem(item,str)	/* gets item into string str			*/
int item;		/* Returns code for previous, next item or quit	*/
char *str;
{
	static int ch;
	static int p,l, len;

	p = par[item].pos;
	len = l = par[item].len;
	yx(STHLIN,(short)p);
	while (l--) {
		while ((ch = gc()) == NULL) ;
		switch (ch) {
		case _CR:	l=0; break;
		case ABORT:	return ABORT;
		case SWITCH:
		case _ESC:	return QUIT;
		case _BS:
			if (len == l+1)	return PRV;
			pc(_BS); pc(ruler[p+len-l-2]); pc(_BS);
			str--;
			l += 2;
			break;
		default:
			pc(ch);
			*str++ = ch;
			break;
		}
	}
	*str = 0;
	return NXT;
}


gtitch(item)	/* returns an item selector character	*/
int item;	/* Note: Status window must be selected	*/
{
	static int ch;

	yx(STHLIN,(short)par[item].pos);
	while ((ch = gc()) == NULL) ;
	return ch;
}


void cpitem(str,item)	/* copies string str as item into status line	*/
char *str;
int item;
{
	static int p,l;

	p = par[item].pos;
	l = par[item].len;
	while (l--)
		ruler[p++] = (*str) ? *str++ : _BLANK;
}


void ptitem(item)	/* Prints item of status line			*/
int item;		/* Note: Status window must already be selected	*/
{
	static int p,l;

	p = par[item].pos;
	l = par[item].len;
	yx(STHLIN,(short)p);
	while (l--)
		pc(ruler[p++]);
}


/* Addresses of cursor coordinates of terminal window	*/

static short *p_lin = 0xFA45;
static short *p_col = 0xFA44;


void selwdw(w)	/* selects window w	*/
int w;
{
	to_wdw(w);			/* switch to window w		*/
	if (w==STAWDW) {
		dissta();		/* disable status line update	*/
		pc(_ESC); pc('R');	/* Dim on			*/
		pc(_FF);		/* clear window			*/
		ps(ruler);		/* display current settings	*/
	} else {
		pc(_ESC); pc('S');	/* Dim off			*/
		if (!(input || output))
			enasta();	/* enable status line upate	*/
		yx(*p_lin,*p_col);	/* cursor to previous position	*/
		pc(_ESC); pc('H');	/* select character set		*/
		pc(cset+cset);
	}
}
