char *connv = "Connect functions CTOS Version-1.00, Summer 1987";

/*  C K C O N U  --  Terminal connection to remote system */

/* modified for CTOS C2.0 by Joel Dunn, UNC-CH, October 1986 */

#include "ctermi.h"

extern int local, speed, escape, duplex, parity, flow, seslog, mdmtyp,
			termtype;
extern char ttname[], sesfil[];

/* Variables global to this module */
int c;			/* c is a character, but must be signed 
			   integer to pass thru -1, which is the
			   modem disconnection signal, and is
			   different from the character 0377 */
int i, active;

char *chstr();

/*  C O N E C T  --  Perform terminal connection  */

conect() {
	int n;
    char errmsg[50], *erp;

	if (!local) {
	    printf("Sorry, you must 'set line' first\n");
	    return(-2);
	}
	if (speed < 0) {
	    printf("Sorry, you must 'set speed' first\n");
	    return(-2);
        }
	if ((escape < 0) || (escape > 0177)) {
	    printf("Your escape character is not ASCII - %d\n",escape);
	    return(-2);
	}
	if (ttopen(ttname,local,mdmtyp) < 0) {
	    erp = errmsg;
	    sprintf(erp,"Sorry, can't open %s",ttname);
	    perror(errmsg);
	    return(-2);
    	}
    	printf("\nConnecting thru %s, speed %d.\r\n",ttname,speed);
	printf("The local escape character is %s (%d).\r\n",chstr(escape),escape);
	printf("Type the local escape character followed by C to get back,\r\n");
	printf("or followed by ? to see other options.\r\n");
	if (seslog) printf("(Session logged to %s.)\r\n",sesfil);

/* Condition console terminal and communication line */	    

    	if (conbin(escape) < 0) {
	    printf("Sorry, can't condition console terminal\n");
	    return(-2);
    	}
	if (ttvt(speed,flow) < 0) {
	    conres();
	    printf("Sorry, Can't condition communication line\n");
	    return(-2);
    	}

/* Select connect function based on terminal type */
	switch (termtype)
		{
		case 0:					/* TTY */
			contty();
			return(0);
		case 1:					/* VT100, routines in ckvt100.c */
			convt100();
			return(0);
		default: 					/* other value */
			printf("\n\nInvalid terminal type.\n\n");
			return(-2);
		}

}

/*  C O N T T Y  --  Connect in TTY mode */
contty()

{
	printf("\n\nConnected in TTY Emulation Mode\n");
	active = 1;
	while (((c = ttinc(0)) >= 0) & active)
		{

/* read, prints port input */

		if (c > 0)
			{
			c &= 0177;		/* Got a char, strip parity. */
			conoc(c);		/* Put it on the screen. */
			if (seslog) zchout(ZSFILE,c);	/* If logging, log it. */
			}

/* read, prints keyboard input */

		c = coninc(0) & 0177;
		if (c == escape) 
			{   	/* Look for escape char */
    			c = coninc(9999) & 0177;
    			doesc(c);
			} 
		else if (c > 0)
				{		/* Ordinary character */
    				ttoc(dopar(c));	/* Send with desired parity */
    				if (duplex) {	/* Half duplex? */
				    conoc(c);	/* Yes, echo it. */
				    /* And maybe log it. */
				    if (seslog) zchout(ZSFILE,c);
				}			
			    }

		}

		if (c < 0) 
			{ /* Comm line hangup detected*/
	    		printf("\r\nCommunications line failure\r\n");
			}
	    conres();			/* Reset the console. */
	    printf("\nTTY Emulation Disconnected\n");
	    return(0);
}

/*  H C O N N E  --  Give help message for connect.  */

hconne() {
    int c;
    static char *hlpmsg[] = {"\
\r\nC to close the connection, or:",
"\r\n  S for status",
"\r\n  ? for help",
"\r\n  B to send a BREAK",
"\r\n  0 to send a null",
"\r\n escape character twice to send the escape character.\r\n\r\n",
"" };

    conola(hlpmsg);			/* Print the help message. */
    conol("Command>");			/* Prompt for command. */
    c = coninc(9999);
    conoc(c);				/* Echo it. */
    conoll("");
    c &= 0177;				/* Strip any parity. */
    return(c);				/* Return it. */
}


/*  C H S T R  --  Make a printable string out of control key */

static char *
chstr(c) int c; {
    static char s[15];
    char *cp = s;

	switch (c)
		{
		case 0:
			sprintf(cp,"HELP");
			break;
		case 1:
			sprintf(cp,"'up arrow'");
			break;
		case 2:
			sprintf(cp,"MARK");
			break;
		case 4:
			sprintf(cp,"FINISH");
			break;
		case 5:
			sprintf(cp,"PREV PAGE");
			break;
		case 7:
			sprintf(cp,"CANCEL");
			break;
		case 8:
			sprintf(cp,"BACKSPACE");
			break;
		case 9:
			sprintf(cp,"TAB");
			break;
		case 10:
			sprintf(cp,"BOUND");
			break;
		case 11:
			sprintf(cp,"'down arrow'");
			break;
		case 12:
			sprintf(cp,"NEXT PAGE");
			break;
		case 13:
			sprintf(cp,"RETURN/NEXT");
			break;
		case 14:
			sprintf(cp,"'left arrow'");
			break;
		case 15:
			sprintf(cp,"MOVE");
			break;
		case 17:
			sprintf(cp,"SCROLL UP");
			break;
		case 18:
			sprintf(cp,"'right arrow'");
			break;
		case 19:
			sprintf(cp,"SCROLL DOWN");
			break;
		case 20:
			sprintf(cp,"COPY");
			break;
		case 21:
			sprintf(cp,"F1");
			break;
		case 22:
			sprintf(cp,"F2");
			break;
		case 23:
			sprintf(cp,"F3");
			break;
		case 24:
			sprintf(cp,"F4");
			break;
		case 25:
			sprintf(cp,"F5");
			break;
		case 26:
			sprintf(cp,"F6");
			break;
		case 27:
			sprintf(cp,"GO");
			break;
		case 28:
			sprintf(cp,"F7");
			break;
		case 29:
			sprintf(cp,"F8");
			break;
		case 30:
			sprintf(cp,"F9");
			break;
		case 31:
			sprintf(cp,"F10");
			break;
		default:
			s[0] = c;
			s[1] = '\0';
			break;
		}

    cp = s;
    return(cp);
}

/*  D O E S C  --  Process an escape character argument  */

doesc(c) char c; {
    int d;
  
    c &= 0177;
    while (1) {
	if (c == escape) {		/* Send escape character */
	    d = dopar(c);
	    ttoc(d);
	    return;
    	} else				/* Or else look it up below. */
	    if (isupper(c)) c = tolower(c);

	switch (c) {

	case 'c':			/* Close connection */
	case '\03':
	    active = 0;
	    conol("\r\n");
	    return;

	case 'b':			/* Send a BREAK */
	case '\02':
	    ttsndb();
	    return;

	case 's':			/* Status */
	case '\023':
	    conol("\r\nConnected thru ");
	    conoll(ttname);
	    if (seslog) {
		conol(", logging to ");
		conol(sesfil);
            }
	    return;

	case '?':			/* Help */
	    c = hconne();
	    continue;

	case '0':			/* Send a null */
	    c = '\0';
	    d = dopar(c);
	    ttoc(d);
	    return;

	case SP:			/* Space, ignore */
	    return;

	default:			/* Other */
	    conoc(BEL); 		/* Invalid esc arg, beep */
	    return;
    	}	    
    }
}    
