#include "fidoterm.h"
#include "mem.h"

/* Setup command. */


static WINDOW setup = {2,18,8,64,A_NORMAL,W_DOUBLEBOX,
PROTOCOL:                    SCREEN:               PARITY:\r"
   Zmodem                       ANSI/VT100            None\r"
   Xmodem CRC                   Hexadecimal           Zero\r"
   Checksum Xmodem              ASCII debug           Odd\r"
   Telink/Modem7                Plain                 Even\r"
   ASCII Text\r"
\r"
   Zmodem auto-download (Yes/No)  . .       Directory . \r"
   File transfer logging (Yes/No) . .       File  . . . \r"
   BERT logging (Yes/No)  . . . . . .       File  . . . \r"
 \r"
   Comm. device (1..4) . . .\r"
   Baud rate (300..38400)  .\r"

   Local echo (Yes/No) . . .\r"

};

static char state = 0;		/* current state in motion[] table below */

static struct {
	char l,c;		/* line/column cursor position */
	char up,down,left,right;/* allowable moves */

} motion[] = {
/* state	move:	up	down	left	right */	
/* 0 */	1,1,		0,	3,	0,	1,
/* 1 */	1,30,		1,	4,	0,	5,
/* 2 */	1,52,		2,	5,	1,	3,
/* 3 */	2,1,		0,	6,	2,	4,
/* 4 */	2,30,		1,	7,	3,	5,
/* 5 */	2,52,		2,	8,	4,	6,
/* 6 */	3,1,		3,	9,	5,	7,
/* 7 */	3,30,		4,	10,	6,	8,
/* 8 */	3,52,		5,	11,	7,	9,
/* 9 */	4,1,		6,	12,	8,	10,
/* 10 */ 4,30,		7,	12,	9,	11,
/* 11 */ 4,52,		9,	15,	10,	12,
/* 12 */ 5,1,		9,	15,	11,	15,
/* 13 */   0,0,		13,	13,	13,	13,
/* 14 */   0,0,		14,	14,	14,	14,
/* 15 */ 7,38,		12,	17,	12,	16,
/* 16 */ 7,56,		15,	18,	15,	17,
/* 17 */ 8,38,		15,	19,	16,	18,
/* 18 */ 8,56,		16,	20,	17,	19,
/* 19 */ 8,38,		17,	21,	18,	20,
/* 20 */ 9,56,		18,	21,	19,	21,
/* 21 */ 11,29,		19,	22,	20,	22,
/* 22 */ 12,29,		21,	23,	21,	23,
/* 23 */ 14,29,		22,	23,	22,	23
};

setup() {
char c;

	init_window(&setup);			/* basic window */
	protocol();				/* protocl settings */
	parity();				/* parity settings */
	terminal();				/* terminal type */

	w_pos(&setup,7,36); w_string(&setup,zmpath);	/* Zmodem autodownload dir */
	w_pos(&setup,8,36); w_string(&setup,xferlog);	/* file transfer log */
	w_pos(&setup,9,36); w_string(&setup,testlog);	/* BERT log file */

	w_pos(&setup,11,36); w_string(&setup,(hdxflg ? "Yes" : "No")); /* local echo */

	w_pos(&setup,13,36); w_printf(&setup,"%d",rate); /* initial baud rate */
	w_pos(&setup,14,36); w_printf(&setup,"%d",iodev); /* initial I/O port */

	switch (keyhit()) {
		case NUL: break;
		case ESC: return;

		case KEY_UP: moveto(motion[state].up); break;
		case KEY_DN: moveto(motion[state].dn); break;
		case KEY_LEFT: moveto(motion[state].left); break;
		case TAB:
		case KEY_RIGHT: moveto(motion[state].right); break;

		default: if ((c >= ' ') && (c <= '~')) dochar(c);
			break;
	}
}

/* Process input for the possible states. */

static dochar(c)
char c;
{
char ans;

	switch (c) {				/* preset for convenience */
		case ' ':
		case 'y':
		case 'Y':
		case CR: ans= 1; break;
		default: ans= 0; break;
	}

	if ((state < 15) && !ans) return;	/* NO case */

	switch (state) {
		case 0: filemode= ZMODEM; protocol(); break;
		case 3: filemode= XMODEMC; protocol(); break;
		case 6: filemode= XMODEM; protocol(); break;
		case 9: filemode= TELINKC; protocol(); break;
		case 12: filemode= ASCII; protocol(); break;

		case 1: termtype= ANSI; terminal(); break;
		case 4: termtype= HEX; terminal(); break;
		case 7: termtype= MONITOR; terminal(); break;
		case 10: termtype= FILTER; terminal(); break;

		case 2: parflg= NOPAR; parity(); break;
		case 5: parflg= ZERPAR; parity(); break;
		case 8: parflg= ODDPAR; parity(); break;
		case 11: parflg= EVNPAR; parity(); break;

		case 15: if (c == ' ') ans= (xxxx ~= 1); else xxxx= ans; goto show;
		case 17: if (c == ' ') ans= (xxxx ~= 1); else xxxx= ans; goto show;
		case 19: if (c == ' ') ans= (xxxx ~= 1); else xxxx= ans; 
show:;			w_pos(&setup,motion[state].l,motion[state].c);
			w_string(&setup,(ans ? "Yes" : "No ")); 
			break;
	}
}

/* Change to a new state, move the cursor there. */

static moveto(s)
char s;
{
	state= s;
	w_pos(&setup,motion[state].l,motion[state].c);
}

/* Display the protocol selection. */

static protocol() {
int i,n;

	switch (filemode) {
		case ZMODEM: n= 0; break;
		case XMODEMC: n= 1; break;
		case XMODEM: n= 2; break;
		case TELINKC: case MODEM7: n= 3; break;
		case ASCII: n= 4; break;
		default: filemode= ASCII; n= 4; break;
	}
	for (i= 0; i < 5; i++) {
		w_pos(&setup,i + 1,1);
		w_char(&setup,(i == n ? '\020' : ' '));
	}
}

/* Display the parity selection. */

static parity() {
int i,n;

	switch (parflg) {
		case NOPAR: n= 0; break;
		case ZERPAR: n= 1; break;
		case ODDPAR: n= 2; break;
		case EVNPAR: n= 3; break;
	}
	for (i= 0; i < 5; i++) {
		w_pos(&setup,i + 1,52);
		w_char(&setup,(i == n ? '\020' : ' '));
	}
}

/* Display the terminal type. */

static terminal() {
int i,n;

	switch (termtype) {
		case ANSI: n= 0; break;
		case HEX: n= 1; break;
		case MONITOR: n= 2; break;
		case FILTER: n= 3; break;
	}
	for (i= 0; i < 5; i++) {
		w_pos(&setup,i + 1,30);
		w_char(&setup,(i == n ? '\020' : ' '));
	}
}


