130043Sminshall /*
230043Sminshall  *	Copyright (c) 1984, 1985, 1986 by the Regents of the
330043Sminshall  *	University of California and by Gregory Glenn Minshall.
430043Sminshall  *
530043Sminshall  *	Permission to use, copy, modify, and distribute these
630043Sminshall  *	programs and their documentation for any purpose and
730043Sminshall  *	without fee is hereby granted, provided that this
830043Sminshall  *	copyright and permission appear on all copies and
930043Sminshall  *	supporting documentation, the name of the Regents of
1030043Sminshall  *	the University of California not be used in advertising
1130043Sminshall  *	or publicity pertaining to distribution of the programs
1230043Sminshall  *	without specific prior permission, and notice be given in
1330043Sminshall  *	supporting documentation that copying and distribution is
1430043Sminshall  *	by permission of the Regents of the University of California
1530043Sminshall  *	and by Gregory Glenn Minshall.  Neither the Regents of the
1630043Sminshall  *	University of California nor Gregory Glenn Minshall make
1730043Sminshall  *	representations about the suitability of this software
1830043Sminshall  *	for any purpose.  It is provided "as is" without
1930043Sminshall  *	express or implied warranty.
2030043Sminshall  */
2130043Sminshall 
2230043Sminshall #ifndef lint
2330043Sminshall static	char	sccsid[] = "@(#)outbound.c	3.1  10/29/86";
2430043Sminshall #endif	/* lint */
2530043Sminshall 
2630043Sminshall 
2730043Sminshall #if defined(unix)
2830043Sminshall #include <signal.h>
2930043Sminshall #include <sgtty.h>
3030043Sminshall #endif
3130043Sminshall #include <stdio.h>
3230043Sminshall #include <curses.h>
3330043Sminshall 
3431179Sminshall #include "../general/general.h"
3531071Sminshall 
3630043Sminshall #include "terminal.h"
3730043Sminshall 
3830043Sminshall #include "../telnet.ext"
3930043Sminshall 
40*31873Sminshall #include "../api/disp_asc.h"
4131224Sminshall 
4230043Sminshall #include "../ctlr/hostctlr.h"
4330043Sminshall #include "../ctlr/inbound.ext"
4431127Sminshall #include "../ctlr/oia.h"
4530043Sminshall #include "../ctlr/options.ext"
4630043Sminshall #include "../ctlr/outbound.ext"
4730043Sminshall #include "../ctlr/screen.h"
4830043Sminshall 
4931179Sminshall #include "../ascii/map3270.ext"
5030043Sminshall 
5131179Sminshall #include "../general/globals.h"
5230043Sminshall 
5330043Sminshall extern void EmptyTerminal();
5430043Sminshall 
5530043Sminshall #define CorrectTerminalCursor() ((TransparentClock == OutputClock)? \
5631839Sminshall 		CursorAddress:UnLocked? CursorAddress: HighestScreen())
5730043Sminshall 
5830043Sminshall 
5930328Sminshall static int terminalCursorAddress;	/* where the cursor is on term */
6030328Sminshall static int screenInitd; 		/* the screen has been initialized */
6130328Sminshall static int screenStopped;		/* the screen has been stopped */
6230043Sminshall static int max_changes_before_poll;	/* how many characters before looking */
6330043Sminshall 					/* at terminal and net again */
6430043Sminshall 
6530328Sminshall static int needToRing;			/* need to ring terinal bell */
6630043Sminshall static char *bellSequence = "\07";	/* bell sequence (may be replaced by
6730043Sminshall 					 * VB during initialization)
6830043Sminshall 					 */
6930328Sminshall static WINDOW *bellwin = 0;		/* The window the bell message is in */
7030043Sminshall int	bellwinup = 0;			/* Are we up with it or not */
7130043Sminshall 
7230043Sminshall #if	defined(unix)
7330043Sminshall static char *KS, *KE;
7430043Sminshall #endif	/* defined(unix) */
7530043Sminshall 
7630074Sminshall 
7730074Sminshall static int inHighlightMode = 0;
7831071Sminshall ScreenImage Terminal[MAXSCREENSIZE];
7930074Sminshall 
8030074Sminshall /* Variables for transparent mode */
8130043Sminshall #if	defined(unix)
8230043Sminshall static int tcflag = -1;			/* transparent mode command flag */
8330043Sminshall static int savefd[2];			/* for storing fds during transcom */
8430074Sminshall extern int	tin, tout;		/* file descriptors */
8530043Sminshall #endif	/* defined(unix) */
8630043Sminshall 
8730730Sminshall 
8830730Sminshall /*
8930730Sminshall  * init_screen()
9030730Sminshall  *
9130730Sminshall  * Initialize variables used by screen.
9230730Sminshall  */
9330730Sminshall 
9430730Sminshall void
9530730Sminshall init_screen()
9630730Sminshall {
9730730Sminshall     bellwinup = 0;
9830730Sminshall     inHighlightMode = 0;
9931071Sminshall     ClearArray(Terminal);
10030730Sminshall }
10130730Sminshall 
10230730Sminshall 
10330043Sminshall /* OurExitString - designed to keep us from going through infinite recursion */
10430043Sminshall 
10530043Sminshall static void
10630043Sminshall OurExitString(file, string, value)
10730043Sminshall FILE	*file;
10830043Sminshall char	*string;
10930043Sminshall int	value;
11030043Sminshall {
11130043Sminshall     static int recursion = 0;
11230043Sminshall 
11330043Sminshall     if (!recursion) {
11430043Sminshall 	recursion = 1;
11530043Sminshall 	ExitString(file, string, value);
11630043Sminshall     }
11730043Sminshall }
11830043Sminshall 
11930043Sminshall 
12030043Sminshall /* DoARefresh */
12130043Sminshall 
12230043Sminshall static void
12330043Sminshall DoARefresh()
12430043Sminshall {
12530043Sminshall     if (ERR == refresh()) {
12630043Sminshall 	OurExitString(stderr, "ERR from refresh\n", 1);
12730043Sminshall     }
12830043Sminshall }
12930043Sminshall 
13030043Sminshall static void
13130043Sminshall GoAway(from, where)
13230043Sminshall char *from;		/* routine that gave error */
13330043Sminshall int	where;		/* cursor address */
13430043Sminshall {
13530043Sminshall 	char foo[100];
13630043Sminshall 
13730043Sminshall 	sprintf(foo, "ERR from %s at %d (%d, %d)\n",
13830043Sminshall 		from, where, ScreenLine(where), ScreenLineOffset(where));
13930043Sminshall 	OurExitString(stderr, foo, 1);
14030043Sminshall 	/* NOTREACHED */
14130043Sminshall }
14230043Sminshall 
14330043Sminshall /* What is the screen address of the attribute byte for the terminal */
14430043Sminshall 
14530043Sminshall static int
14630043Sminshall WhereTermAttrByte(p)
14730043Sminshall register int	p;
14830043Sminshall {
14930043Sminshall     register int i;
15030043Sminshall 
15130043Sminshall     i = p;
15230043Sminshall 
15330043Sminshall     do {
15430043Sminshall 	if (TermIsStartField(i)) {
15530043Sminshall 	    return(i);
15630043Sminshall 	}
15730043Sminshall 	i = ScreenDec(i);
15830043Sminshall     } while (i != p);
15930043Sminshall 
16030043Sminshall     return(LowestScreen());	/* unformatted screen... */
16130043Sminshall }
16230043Sminshall 
16330043Sminshall /*
16430043Sminshall  *	There are two algorithms for updating the screen.
16530043Sminshall  *  The first, SlowScreen() optimizes the line between the
16630043Sminshall  *  computer and the screen (say a 9600 baud line).  To do
16730043Sminshall  *  this, we break out of the loop every so often to look
16830043Sminshall  *  at any pending input from the network (so that successive
16930043Sminshall  *  screens will only partially print until the final screen,
17030043Sminshall  *  the one the user possibly wants to see, is displayed
17130043Sminshall  *  in its entirety).
17230043Sminshall  *
17330043Sminshall  *	The second algorithm tries to optimize CPU time (by
17430043Sminshall  *  being simpler) at the cost of the bandwidth to the
17530043Sminshall  *  screen.
17630043Sminshall  *
17730043Sminshall  *	Of course, curses(3X) gets in here also.
17830043Sminshall  */
17930043Sminshall 
18030043Sminshall 
18130043Sminshall #if	defined(NOT43)
18230043Sminshall static int
18330043Sminshall #else	/* defined(NOT43) */
18430043Sminshall static void
18530043Sminshall #endif	/* defined(NOT43) */
18630043Sminshall SlowScreen()
18730043Sminshall {
18830043Sminshall     register int pointer;
18930043Sminshall     register int c;
19030043Sminshall     register int fieldattr;
19130043Sminshall     register int columnsleft;
19230043Sminshall 
19330043Sminshall #   define  SetHighlightMode(p) { \
19430043Sminshall 		if (!IsStartField(p) && IsHighlightedAttr(fieldattr)) { \
19530043Sminshall 		    if (!inHighlightMode) { \
19630043Sminshall 			inHighlightMode = 1; \
19730043Sminshall 			standout(); \
19830043Sminshall 		    } \
19930043Sminshall 		} else { \
20030043Sminshall 		    if (inHighlightMode) { \
20130043Sminshall 			inHighlightMode = 0; \
20230043Sminshall 			standend(); \
20330043Sminshall 		    } \
20430043Sminshall 		} \
20530043Sminshall 	    }
20630043Sminshall 
20730043Sminshall #   define  DoCharacterAt(c,p) { \
20830043Sminshall 		SetTerminal(p, c); \
20930043Sminshall 		if (p != HighestScreen()) { \
21030043Sminshall 		    c = TerminalCharacterAttr(disp_asc[c&0xff], p, \
21130043Sminshall 								fieldattr); \
21230043Sminshall 		    if (terminalCursorAddress != p) { \
21330043Sminshall 			if (ERR == mvaddch(ScreenLine(p), \
21430043Sminshall 						ScreenLineOffset(p), c)) {\
21530043Sminshall 			    GoAway("mvaddch", p); \
21630043Sminshall 			} \
21730043Sminshall 		    } else { \
21830043Sminshall 			if (ERR == addch(c)) {\
21930043Sminshall 			    GoAway("addch", p); \
22030043Sminshall 			} \
22130043Sminshall 		    } \
22230043Sminshall 		    terminalCursorAddress = ScreenInc(p); \
22330043Sminshall 		} \
22430043Sminshall 	    }
22530043Sminshall 
22630043Sminshall 
22730043Sminshall     /* run through screen, printing out non-null lines */
22830043Sminshall 
22930043Sminshall     /* There are two separate reasons for wanting to terminate this
23030043Sminshall      * loop early.  One is to respond to new input (either from
23130043Sminshall      * the terminal or from the network [host]).  For this reason,
23230043Sminshall      * we expect to see 'HaveInput' come true when new input comes in.
23330043Sminshall      *
23430043Sminshall      * The second reason is a bit more difficult (for me) to understand.
23530043Sminshall      * Basically, we don't want to get too far ahead of the characters that
23630043Sminshall      * appear on the screen.  Ideally, we would type out a few characters,
23730043Sminshall      * wait until they appeared on the screen, then type out a few more.
23830043Sminshall      * The reason for this is that the user, on seeing some characters
23930043Sminshall      * appear on the screen may then start to type something.  We would
24030043Sminshall      * like to look at what the user types at about the same 'time'
24130043Sminshall      * (measured by characters being sent to the terminal) that the
24230043Sminshall      * user types them.  For this reason, what we would like to do
24330043Sminshall      * is update a bit, then call curses to do a refresh, flush the
24430043Sminshall      * output to the terminal, then wait until the terminal data
24530043Sminshall      * has been sent.
24630043Sminshall      *
24730043Sminshall      * Note that curses is useful for, among other things, deciding whether
24830043Sminshall      * or not to send :ce: (clear to end of line), so we should call curses
24930043Sminshall      * at end of lines (beginning of next lines).
25030043Sminshall      *
25130043Sminshall      * The problems here are the following:  If we do lots of write(2)s,
25230043Sminshall      * we will be doing lots of context switches, thus lots of overhead
25330043Sminshall      * (which we have already).  Second, if we do a select to wait for
25430043Sminshall      * the output to drain, we have to contend with the fact that NOW
25530043Sminshall      * we are scheduled to run, but who knows what the scheduler will
25630043Sminshall      * decide when the output has caught up.
25730043Sminshall      */
25830043Sminshall 
25930043Sminshall     if (Highest == HighestScreen()) {
26030043Sminshall 	Highest = ScreenDec(Highest);	/* else, while loop will never end */
26130043Sminshall     }
26230043Sminshall     if (Lowest < LowestScreen()) {
26330043Sminshall 	Lowest = LowestScreen();	/* could be -1 in some cases with
26430043Sminshall 					 * unformatted screens.
26530043Sminshall 					 */
26630043Sminshall     }
26730043Sminshall     if (Highest >= (pointer = Lowest)) {
26830043Sminshall 		/* if there is anything to do, do it.  We won't terminate
26930043Sminshall 		 * the loop until we've gone at least to Highest.
27030043Sminshall 		 */
27130043Sminshall 	while ((pointer <= Highest) && !HaveInput) {
27230043Sminshall 
27330043Sminshall 		/* point at the next place of disagreement */
27430043Sminshall 	    pointer += (bunequal(Host+pointer, Terminal+pointer,
27530043Sminshall 			(Highest-pointer+1)*sizeof Host[0])/sizeof Host[0]);
27630043Sminshall 
27730043Sminshall 		/* how many characters to change until the end of the
27830043Sminshall 		 * current line
27930043Sminshall 		 */
28030043Sminshall 	    columnsleft = NumberColumns - ScreenLineOffset(pointer);
28130043Sminshall 		/*
28230043Sminshall 		 * Make sure we are where we think we are.
28330043Sminshall 		 */
28430043Sminshall 	    move(ScreenLine(pointer), ScreenLineOffset(pointer));
28530043Sminshall 
28630043Sminshall 		/* what is the field attribute of the current position */
28730043Sminshall 	    fieldattr = FieldAttributes(WhereAttrByte(pointer));
28830043Sminshall 
28930043Sminshall 	    if ((IsStartField(pointer) != TermIsStartField(pointer)) ||
29030043Sminshall 		    (IsStartField(pointer) &&
29130043Sminshall 			fieldattr != TermAttributes(pointer))) {
29230043Sminshall 
29330043Sminshall 		int oldterm;
29430043Sminshall 
29530043Sminshall 		oldterm = TermAttributes(pointer);
29630043Sminshall 		if (IsStartField(pointer)) {
29730043Sminshall 		    TermNewField(pointer, fieldattr);
29830043Sminshall 		    SetTerminal(pointer, 0);
29930043Sminshall 		} else {
30030043Sminshall 		    TermDeleteField(pointer);
30130043Sminshall 		}
30230043Sminshall 		    /* We always do the first character in a divergent
30330043Sminshall 		     * field, since otherwise the start of a field in
30430043Sminshall 		     * the Host structure may leave a highlighted blank
30530043Sminshall 		     * on the screen, and the start of a field in the
30630043Sminshall 		     * Terminal structure may leave a non-highlighted
30730043Sminshall 		     * something in the middle of a highlighted field
30830043Sminshall 		     * on the screen.
30930043Sminshall 		     */
31030043Sminshall 		SetHighlightMode(pointer);
31130043Sminshall 		c = GetHost(pointer);
31230043Sminshall 		DoCharacterAt(c,pointer);		/* MACRO */
31330043Sminshall 
31430043Sminshall 		if (NotVisuallyCompatibleAttributes
31530043Sminshall 				(pointer, fieldattr, oldterm)) {
31630043Sminshall 		    int j;
31730043Sminshall 
31830043Sminshall 		    j = pointer;
31930043Sminshall 
32030043Sminshall 		    pointer = ScreenInc(pointer);
32130043Sminshall 		    if (!(--columnsleft)) {
32230043Sminshall 			DoARefresh();
32330043Sminshall 			EmptyTerminal();
32430043Sminshall 			move(ScreenLine(pointer), 0);
32530043Sminshall 			columnsleft = NumberColumns;
32630043Sminshall 		    }
32730043Sminshall 		    SetHighlightMode(pointer);	/* Turn on highlighting */
32831452Sminshall 		    while ((!IsStartField(pointer)) &&
32931452Sminshall 				(!TermIsStartField(pointer))) {
33030043Sminshall 			c = GetHost(pointer);
33130043Sminshall 			DoCharacterAt(c,pointer);	/* MACRO */
33230043Sminshall 			pointer = ScreenInc(pointer);
33330043Sminshall 			if (!(--columnsleft)) {
33430043Sminshall 			    DoARefresh();
33530043Sminshall 			    EmptyTerminal();
33630043Sminshall 			    move(ScreenLine(pointer), 0);
33730043Sminshall 			    columnsleft = NumberColumns;
33830043Sminshall 				/* We don't look at HaveInput here, since
33930043Sminshall 				 * if we leave this loop before the end of
34030043Sminshall 				 * the 3270 field, we could have pointer
34130043Sminshall 				 * higher than Highest.  This would cause
34230043Sminshall 				 * us to end the highest "while" loop,
34330043Sminshall 				 * but we may, in fact, need to go around the
34430043Sminshall 				 * screen once again.
34530043Sminshall 				 */
34630043Sminshall 			}
34730043Sminshall 			/*		The loop needs to be protected
34830043Sminshall 			 *	from the situation where there had been only
34930043Sminshall 			 *	one field on the Terminal, and none on the Host.
35030043Sminshall 			 *	In this case, we have just deleted our last
35130043Sminshall 			 *	field.	Hence, the break.
35230043Sminshall 			 */
35330043Sminshall 			if (j == pointer) {
35430043Sminshall 			    break;
35530043Sminshall 			}
35630043Sminshall 		    }
35730043Sminshall 		    if (IsStartField(pointer) && !TermIsStartField(pointer)) {
35830043Sminshall 			    /* Remember what the terminal looked like */
35930043Sminshall 			TermNewField(pointer, oldterm);
36030043Sminshall 			    /*
36130043Sminshall 			     * The danger here is that the current position may
36230043Sminshall 			     * be the start of a Host field.  If so, and the
36330043Sminshall 			     * field is highlighted, and our terminal was
36430043Sminshall 			     * highlighted, then we will leave a highlighted
36530043Sminshall 			     * blank at this position.
36630043Sminshall 			     */
36730043Sminshall 			SetHighlightMode(pointer);
36830043Sminshall 			c = GetHost(pointer);
36930043Sminshall 			DoCharacterAt(c,pointer);
37030043Sminshall 		    }
37130043Sminshall 			/* We could be in the situation of needing to exit.
37230043Sminshall 			 * This could happen if the current field wrapped around
37330043Sminshall 			 * the end of the screen.
37430043Sminshall 			 */
37530043Sminshall 		    if (j > pointer) {
37631452Sminshall 			/*
37731452Sminshall 			 * pointer is guaranteed to be higher than Highest...
37831452Sminshall 			 */
37931452Sminshall 			pointer = Highest+1;	/* We did the highest thing */
38030043Sminshall 			break;
38130043Sminshall 		    }
38230043Sminshall 		} else {
38330043Sminshall 		    c = GetHost(pointer);
38430043Sminshall 			/* We always do the first character in a divergent
38530043Sminshall 			 * field, since otherwise the start of a field in
38630043Sminshall 			 * the Host structure may leave a highlighted blank
38730043Sminshall 			 * on the screen, and the start of a field in the
38830043Sminshall 			 * Terminal structure may leave a non-highlighted
38930043Sminshall 			 * something in the middle of a highlighted field
39030043Sminshall 			 * on the screen.
39130043Sminshall 			 */
39230043Sminshall 		    SetHighlightMode(pointer);
39330043Sminshall 		    DoCharacterAt(c,pointer);
39430043Sminshall 		}
39530043Sminshall 	    } else {
39630043Sminshall 		SetHighlightMode(pointer);
39730054Sminshall 		/*
39830054Sminshall 		 * The following will terminate at least when we get back
39930043Sminshall 		 * to the original 'pointer' location (since we force
40030043Sminshall 		 * things to be equal).
40130043Sminshall 		 */
40230043Sminshall 		while (((c = GetHost(pointer)) != GetTerminal(pointer)) &&
40330043Sminshall 			!IsStartField(pointer) && !TermIsStartField(pointer)) {
40430043Sminshall 		    DoCharacterAt(c, pointer);
40530043Sminshall 		    pointer = ScreenInc(pointer);
40630043Sminshall 		    if (!(--columnsleft)) {
40730043Sminshall 			DoARefresh();
40830043Sminshall 			EmptyTerminal();
40930043Sminshall 			if (HaveInput) {	/* if input came in, take it */
41030043Sminshall 			    break;
41130043Sminshall 			}
41230043Sminshall 			move(ScreenLine(pointer), 0);
41330043Sminshall 			columnsleft = NumberColumns;
41430043Sminshall 		    }
41530043Sminshall 		}
41630043Sminshall 	    }
41730043Sminshall 	}
41830043Sminshall     }
41930043Sminshall     DoARefresh();
42030043Sminshall     Lowest = pointer;
42130043Sminshall     if (Lowest > Highest) {		/* if we finished input... */
42230043Sminshall 	Lowest = HighestScreen()+1;
42330043Sminshall 	Highest = LowestScreen()-1;
42430043Sminshall 	terminalCursorAddress = CorrectTerminalCursor();
42530043Sminshall 	if (ERR == move(ScreenLine(terminalCursorAddress),
42630043Sminshall 			ScreenLineOffset(terminalCursorAddress))) {
42730043Sminshall 	    GoAway("move", terminalCursorAddress);
42830043Sminshall 	}
42930043Sminshall 	DoARefresh();
43030043Sminshall 	if (needToRing) {
43130043Sminshall 	    StringToTerminal(bellSequence);
43230043Sminshall 	    needToRing = 0;
43330043Sminshall 	}
43430043Sminshall     }
43530043Sminshall     EmptyTerminal();			/* move data along */
43630043Sminshall     return;
43730043Sminshall }
43830043Sminshall 
43930043Sminshall #if	defined(NOT43)
44030043Sminshall static int
44130043Sminshall #else	/* defined(NOT43) */
44230043Sminshall static void
44330043Sminshall #endif	/* defined(NOT43) */
44430043Sminshall FastScreen()
44530043Sminshall {
44631101Sminshall #if	defined(MSDOS)
44730043Sminshall #define	SaveCorner	0
44831101Sminshall #else	/* defined(MSDOS) */
44930043Sminshall #define	SaveCorner	1
45031101Sminshall #endif	/* defined(MSDOS) */
45130043Sminshall 
45230043Sminshall #define	DoAttribute(a) 	    if (IsHighlightedAttr(a)) { \
45330043Sminshall 				standout(); \
45430043Sminshall 			    } else { \
45530043Sminshall 				standend(); \
45630043Sminshall 			    } \
45730043Sminshall 			    if (IsNonDisplayAttr(a)) { \
45830043Sminshall 				a = 0; 	/* zero == don't display */ \
45930043Sminshall 			    } \
46030043Sminshall 			    if (!FormattedScreen()) { \
46130043Sminshall 				a = 1;	/* one ==> do display on unformatted */\
46230043Sminshall 			    }
46330043Sminshall     ScreenImage *p, *upper;
46430043Sminshall     int fieldattr;		/* spends most of its time == 0 or 1 */
46530043Sminshall 
46630043Sminshall /* OK.  We want to do this a quickly as possible.  So, we assume we
46730043Sminshall  * only need to go from Lowest to Highest.  However, if we find a
46830043Sminshall  * field in the middle, we do the whole screen.
46930043Sminshall  *
47030043Sminshall  * In particular, we separate out the two cases from the beginning.
47130043Sminshall  */
47230043Sminshall     if ((Highest != HighestScreen()) || (Lowest != LowestScreen())) {
47330043Sminshall 	register int columnsleft;
47430043Sminshall 
47530043Sminshall 	move(ScreenLine(Lowest), ScreenLineOffset(Lowest));
47630043Sminshall 	p = &Host[Lowest];
47731101Sminshall #if	!defined(MSDOS)
47830043Sminshall 	if (Highest == HighestScreen()) {
47930043Sminshall 	    Highest = ScreenDec(Highest);
48030043Sminshall 	}
48131101Sminshall #endif	/* !defined(MSDOS) */
48230043Sminshall 	upper = &Host[Highest];
48330043Sminshall 	fieldattr = FieldAttributes(Lowest);
48430043Sminshall 	DoAttribute(fieldattr);	/* Set standout, non-display status */
48530043Sminshall 	columnsleft = NumberColumns-ScreenLineOffset(p-Host);
48630043Sminshall 
48730043Sminshall 	while (p <= upper) {
48830054Sminshall 	    if (IsStartFieldPointer(p)) {	/* New field? */
48930043Sminshall 		Highest = HighestScreen();
49030043Sminshall 		Lowest = LowestScreen();
49130043Sminshall 		FastScreen();		/* Recurse */
49230043Sminshall 		return;
49330043Sminshall 	    } else if (fieldattr) {	/* Should we display? */
49431148Sminshall 			    /* Display translated data */
49531148Sminshall 		addch(disp_asc[GetTerminalPointer(p)]);
49630043Sminshall 	    } else {
49730043Sminshall 		addch(' ');			/* Display a blank */
49830043Sminshall 	    }
49930043Sminshall 			/* If the physical screen is larger than what we
50030043Sminshall 			 * are using, we need to make sure that each line
50130043Sminshall 			 * starts at the beginning of the line.  Otherwise,
50230043Sminshall 			 * we will just string all the lines together.
50330043Sminshall 			 */
50430043Sminshall 	    p++;
50530043Sminshall 	    if (--columnsleft == 0) {
50630043Sminshall 		int i = p-Host;
50730043Sminshall 
50830043Sminshall 		move(ScreenLine(i), 0);
50930043Sminshall 		columnsleft = NumberColumns;
51030043Sminshall 	    }
51130043Sminshall 	}
51230043Sminshall     } else {		/* Going from Lowest to Highest */
51330043Sminshall 	unsigned char tmpbuf[MAXNUMBERCOLUMNS+1];
51430043Sminshall 	ScreenImage *End = &Host[ScreenSize]-1-SaveCorner;
51530043Sminshall 	register unsigned char *tmp = tmpbuf, *tmpend = tmpbuf+NumberColumns;
51630043Sminshall 
51730043Sminshall 	*tmpend = 0;		/* terminate from the beginning */
51830043Sminshall 	move(0,0);
51930043Sminshall 	p = Host;
52030043Sminshall 	fieldattr = FieldAttributes(LowestScreen());
52130043Sminshall 	DoAttribute(fieldattr);	/* Set standout, non-display status */
52230043Sminshall 
52330043Sminshall 	while (p <= End) {
52430054Sminshall 	    if (IsStartFieldPointer(p)) {	/* New field? */
52530043Sminshall 		if (tmp != tmpbuf) {
52630043Sminshall 		    *tmp++ = 0;			/* close out */
52730043Sminshall 		    addstr(tmpbuf);
52830043Sminshall 		    tmp = tmpbuf;
52930043Sminshall 		    tmpend = tmpbuf + NumberColumns - ScreenLineOffset(p-Host);
53030043Sminshall 		}
53130054Sminshall 		fieldattr = FieldAttributesPointer(p);	/* Get attributes */
53230043Sminshall 		DoAttribute(fieldattr);	/* Set standout, non-display */
53330043Sminshall 		*tmp++ = ' ';
53430043Sminshall 	    } else {
53530043Sminshall 		if (fieldattr) {	/* Should we display? */
53630043Sminshall 				/* Display translated data */
53731148Sminshall 		    *tmp++ = disp_asc[GetTerminalPointer(p)];
53830043Sminshall 		} else {
53930043Sminshall 		    *tmp++ = ' ';
54030043Sminshall 		}
54130043Sminshall 	    }
54230043Sminshall 			/* If the physical screen is larger than what we
54330043Sminshall 			 * are using, we need to make sure that each line
54430043Sminshall 			 * starts at the beginning of the line.  Otherwise,
54530043Sminshall 			 * we will just string all the lines together.
54630043Sminshall 			 */
54730043Sminshall 	    p++;
54830043Sminshall 	    if (tmp == tmpend) {
54930043Sminshall 		int i = p-Host;		/* Be sure the "p++" happened first! */
55030043Sminshall 
55130043Sminshall 		*tmp++ = 0;
55230043Sminshall 		addstr(tmpbuf);
55330043Sminshall 		tmp = tmpbuf;
55430043Sminshall 		move(ScreenLine(i), 0);
55530043Sminshall 		tmpend = tmpbuf + NumberColumns;
55630043Sminshall 	    }
55730043Sminshall 	}
55830043Sminshall 	if (tmp != tmpbuf) {
55930043Sminshall 	    *tmp++ = 0;
56030043Sminshall 	    addstr(tmpbuf);
56130043Sminshall 	    tmp = tmpbuf;
56230043Sminshall 	}
56330043Sminshall     }
56430043Sminshall     Lowest = HighestScreen()+1;
56530043Sminshall     Highest = LowestScreen()-1;
56630043Sminshall     terminalCursorAddress = CorrectTerminalCursor();
56730043Sminshall     if (ERR == move(ScreenLine(terminalCursorAddress),
56830043Sminshall 		    ScreenLineOffset(terminalCursorAddress))) {
56930043Sminshall 	GoAway("move", terminalCursorAddress);
57030043Sminshall     }
57130043Sminshall     DoARefresh();
57230043Sminshall     if (needToRing) {
57330043Sminshall 	StringToTerminal(bellSequence);
57430043Sminshall 	needToRing = 0;
57530043Sminshall     }
57630043Sminshall     EmptyTerminal();			/* move data along */
57730043Sminshall     return;
57830043Sminshall }
57930043Sminshall 
58030043Sminshall 
58130043Sminshall /* TryToSend - send data out to user's terminal */
58230043Sminshall 
58330043Sminshall #if	defined(NOT43)
58430043Sminshall int
58530043Sminshall #else	/* defined(NOT43) */
58630043Sminshall void
58730043Sminshall #endif	/* defined(NOT43) */
58830043Sminshall 	(*TryToSend)() = FastScreen;
58930043Sminshall 
59031127Sminshall void
59131127Sminshall ScreenOIA(oia)
59231127Sminshall OIA *oia;
59331127Sminshall {
59431127Sminshall }
59531127Sminshall 
59631127Sminshall 
59730328Sminshall /* InitTerminal - called to initialize the screen, etc. */
59830043Sminshall 
59930043Sminshall void
60030328Sminshall InitTerminal()
60130043Sminshall {
60230043Sminshall #if defined(unix)
60330043Sminshall     struct sgttyb ourttyb;
60430043Sminshall     static int speeds[] = { 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
60530043Sminshall 		2400, 4800, 9600 };
60630043Sminshall #endif
60730043Sminshall 
60830328Sminshall     InitMapping();		/* Go do mapping file (MAP3270) first */
60930043Sminshall     if (!screenInitd) { 	/* not initialized */
61030043Sminshall #if	defined(unix)
61130043Sminshall 	char KSEbuffer[2050];
61230043Sminshall 	char *lotsofspace = KSEbuffer;
61330043Sminshall 	extern int abort();
61430043Sminshall 	extern char *tgetstr();
61530043Sminshall #endif	/* defined(unix) */
61630043Sminshall 
61731101Sminshall 	ClearArray(Terminal);
61830328Sminshall 	terminalCursorAddress = SetBufferAddress(0,0);
61930043Sminshall #if defined(unix)
62030043Sminshall 	signal(SIGHUP, abort);
62130043Sminshall #endif
62230043Sminshall 
62330043Sminshall 	TryToSend = FastScreen;
62431559Sminshall #if defined(unix)
62530043Sminshall 	ioctl(1, TIOCGETP, (char *) &ourttyb);
62630043Sminshall 	if ((ourttyb.sg_ospeed < 0) || (ourttyb.sg_ospeed > B9600)) {
62730043Sminshall 	    max_changes_before_poll = 1920;
62830043Sminshall 	} else {
62930043Sminshall 	    max_changes_before_poll = speeds[ourttyb.sg_ospeed]/10;
63030043Sminshall 	    if (max_changes_before_poll < 40) {
63130043Sminshall 		max_changes_before_poll = 40;
63230043Sminshall 	    }
63330043Sminshall 	    TryToSend = SlowScreen;
63430043Sminshall 	    HaveInput = 1;		/* get signals going */
63530043Sminshall 	}
63631559Sminshall #endif	/* defined(unix) */
63730043Sminshall 	setcommandmode();
63830043Sminshall 	/*
63930043Sminshall 	 * By now, initscr() (in curses) has been called (from telnet.c),
64030043Sminshall 	 * and the screen has been initialized.
64130043Sminshall 	 */
64230043Sminshall #if defined(unix)
64330043Sminshall 	nonl();
64430043Sminshall 			/* the problem is that curses catches SIGTSTP to
64530043Sminshall 			 * be nice, but it messes us up.
64630043Sminshall 			 */
64730043Sminshall 	signal(SIGTSTP, SIG_DFL);
64830043Sminshall 	if ((KS = tgetstr("ks", &lotsofspace)) != 0) {
64930043Sminshall 	    KS = strsave(KS);
65030043Sminshall 	    StringToTerminal(KS);
65130043Sminshall 	}
65230043Sminshall 	if ((KE = tgetstr("ke", &lotsofspace)) != 0) {
65330043Sminshall 	    KE = strsave(KE);
65430043Sminshall 	}
65530043Sminshall 	if (tgetstr("md", &lotsofspace) && tgetstr("me", &lotsofspace)) {
65630043Sminshall 	   SO = strsave(tgetstr("md", &lotsofspace));
65730043Sminshall 	   SE = strsave(tgetstr("me", &lotsofspace));
65830043Sminshall 	}
65930043Sminshall #endif
66030043Sminshall 	DoARefresh();
66130043Sminshall 	setconnmode();
66230043Sminshall 	if (VB && *VB) {
66330043Sminshall 	    bellSequence = VB;		/* use visual bell */
66430043Sminshall 	}
66530043Sminshall 	screenInitd = 1;
66630043Sminshall 	screenStopped = 0;		/* Not stopped */
66730043Sminshall     }
66830043Sminshall }
66930043Sminshall 
67030043Sminshall 
67130043Sminshall /* StopScreen - called when we are going away... */
67230043Sminshall 
67330043Sminshall void
67430043Sminshall StopScreen(doNewLine)
67530043Sminshall int doNewLine;
67630043Sminshall {
67730043Sminshall     if (screenInitd && !screenStopped) {
67830043Sminshall 	move(NumberLines-1, 1);
67930043Sminshall 	standend();
68030043Sminshall 	inHighlightMode = 0;
68130043Sminshall 	DoARefresh();
68230043Sminshall 	setcommandmode();
68330043Sminshall 	endwin();
68430043Sminshall 	setconnmode();
68530043Sminshall #if	defined(unix)
68630043Sminshall 	if (KE) {
68730043Sminshall 	    StringToTerminal(KE);
68830043Sminshall 	}
68930043Sminshall #endif	/* defined(unix) */
69030043Sminshall 	if (doNewLine) {
69130043Sminshall 	    StringToTerminal("\r\n");
69230043Sminshall 	}
69330043Sminshall 	EmptyTerminal();
69430043Sminshall 	screenStopped = 1;		/* This is stopped */
69530043Sminshall     }
69630043Sminshall }
69730043Sminshall 
69830043Sminshall 
69930043Sminshall /* RefreshScreen - called to cause the screen to be refreshed */
70030043Sminshall 
70130043Sminshall void
70230043Sminshall RefreshScreen()
70330043Sminshall {
70430043Sminshall     clearok(curscr, TRUE);
70530043Sminshall     (*TryToSend)();
70630043Sminshall }
70730043Sminshall 
70830043Sminshall 
70930043Sminshall /* ConnectScreen - called to reconnect to the screen */
71030043Sminshall 
71130043Sminshall void
71230043Sminshall ConnectScreen()
71330043Sminshall {
71430043Sminshall     if (screenInitd) {
71530043Sminshall #if	defined(unix)
71630043Sminshall 	if (KS) {
71730043Sminshall 	    StringToTerminal(KS);
71830043Sminshall 	}
71930043Sminshall #endif	/* defined(unix) */
72030043Sminshall 	RefreshScreen();
72130043Sminshall 	(*TryToSend)();
72230043Sminshall 	screenStopped = 0;
72330043Sminshall     }
72430043Sminshall }
72530043Sminshall 
72630043Sminshall /* LocalClearScreen() - clear the whole ball of wax, cheaply */
72730043Sminshall 
72830043Sminshall void
72930043Sminshall LocalClearScreen()
73030043Sminshall {
73130043Sminshall     outputPurge();		/* flush all data to terminal */
73230043Sminshall     clear();			/* clear in curses */
73331101Sminshall     ClearArray(Terminal);
73430043Sminshall     Clear3270();
73530043Sminshall     Lowest = HighestScreen()+1; /* everything in sync... */
73630043Sminshall     Highest = LowestScreen()+1;
73730043Sminshall }
73830043Sminshall 
73930043Sminshall 
74030043Sminshall void
74130043Sminshall BellOff()
74230043Sminshall {
74330043Sminshall     if (bellwinup) {
74430043Sminshall 	delwin(bellwin);
74530043Sminshall 	bellwin = 0;
74630043Sminshall 	bellwinup = 0;
74730043Sminshall 	touchwin(stdscr);
74830043Sminshall 	DoARefresh();
74930043Sminshall     }
75030043Sminshall }
75130043Sminshall 
75230043Sminshall 
75330043Sminshall void
75430043Sminshall RingBell(s)
75530043Sminshall char *s;
75630043Sminshall {
75730043Sminshall     needToRing = 1;
75830043Sminshall     if (s) {
75930043Sminshall 	int len = strlen(s);
76030043Sminshall 
76130043Sminshall 	if (len > COLS-2) {
76230043Sminshall 	    len = COLS-2;
76330043Sminshall 	}
76430043Sminshall 	if ((bellwin = newwin(3, len+2, LINES/2, 0)) == NULL) {
76530043Sminshall 	    OurExitString(stderr, "Error from newwin in RingBell", 1);
76630043Sminshall 	}
76730043Sminshall 	werase(bellwin);
76830043Sminshall 	wstandout(bellwin);
76930043Sminshall 	box(bellwin, '|', '-');
77030043Sminshall 	if (wmove(bellwin, 1, 1) == ERR) {
77130043Sminshall 	    OurExitString(stderr, "Error from wmove in RingBell", 1);
77230043Sminshall 	}
77330043Sminshall 	while (len--) {
77430043Sminshall 	    if (waddch(bellwin, *s++) == ERR) {
77530043Sminshall 		OurExitString(stderr, "Error from waddch in RingBell", 1);
77630043Sminshall 	    }
77730043Sminshall 	}
77830043Sminshall 	wstandend(bellwin);
77930043Sminshall 	if (wrefresh(bellwin) == ERR) {
78030043Sminshall 	    OurExitString(stderr, "Error from wrefresh in RingBell", 1);
78130043Sminshall 	}
78230043Sminshall 	bellwinup = 1;
78330043Sminshall     }
78430043Sminshall }
78530043Sminshall 
78630043Sminshall 
78730043Sminshall /* returns a 1 if no more output available (so, go ahead and block),
78830043Sminshall     or a 0 if there is more output available (so, just poll the other
78930043Sminshall     sources/destinations, don't block).
79030043Sminshall  */
79130043Sminshall 
79230043Sminshall int
79330043Sminshall DoTerminalOutput()
79430043Sminshall {
79530043Sminshall 	/* called just before a select to conserve IO to terminal */
79631462Sminshall     if (!(screenInitd||screenStopped)) {
79730369Sminshall 	return 1;		/* No output if not initialized */
79830369Sminshall     }
79930369Sminshall     if ((Lowest <= Highest) || needToRing ||
80030369Sminshall 			(terminalCursorAddress != CorrectTerminalCursor())) {
80130043Sminshall 	(*TryToSend)();
80230043Sminshall     }
80330043Sminshall     if (Lowest > Highest) {
80430369Sminshall 	return 1;		/* no more output now */
80530043Sminshall     } else {
80630369Sminshall 	return 0;		/* more output for future */
80730043Sminshall     }
80830043Sminshall }
80930074Sminshall 
81030074Sminshall /*
81130074Sminshall  * The following are defined to handle transparent data.
81230074Sminshall  */
81330074Sminshall 
81430074Sminshall void
81530074Sminshall TransStop()
81630074Sminshall {
81730074Sminshall #if	defined(unix)
81830074Sminshall     if (tcflag == 0) {
81930074Sminshall        tcflag = -1;
82030074Sminshall        (void) signal(SIGCHLD, SIG_DFL);
82130074Sminshall     } else if (tcflag > 0) {
82230074Sminshall        setcommandmode();
82330074Sminshall        (void) close(tin);
82430074Sminshall        (void) close(tout);
82530074Sminshall        tin = savefd[0];
82630074Sminshall        tout = savefd[1];
82730074Sminshall        setconnmode();
82830074Sminshall        tcflag = -1;
82930074Sminshall        (void) signal(SIGCHLD, SIG_DFL);
83030074Sminshall     }
83130074Sminshall #endif	/* defined(unix) */
83230074Sminshall     RefreshScreen();
83330074Sminshall }
83430074Sminshall 
83530074Sminshall void
83631863Sminshall TransOut(buffer, count, kind, control)
83730074Sminshall unsigned char	*buffer;
83830074Sminshall int		count;
83931863Sminshall int		kind;		/* 0 or 5 */
84031863Sminshall int		control;	/* To see if we are done */
84130074Sminshall {
84230074Sminshall #if	defined(unix)
84330074Sminshall     extern char *transcom;
84430074Sminshall     int inpipefd[2], outpipefd[2], savemode;
84530074Sminshall     void aborttc();
84630074Sminshall #endif	/* defined(unix) */
84730074Sminshall 
84830074Sminshall     while (DoTerminalOutput() == 0) {
84930074Sminshall #if defined(unix)
85030074Sminshall 	HaveInput = 0;
85130074Sminshall #endif /* defined(unix) */
85230074Sminshall     }
85330074Sminshall #if	defined(unix)
85430074Sminshall     if (transcom && tcflag == -1) {
85530074Sminshall        while (1) {			  /* go thru once */
85630074Sminshall 	     if (pipe(outpipefd) < 0) {
85730074Sminshall 		break;
85830074Sminshall 	     }
85930074Sminshall 	     if (pipe(inpipefd) < 0) {
86030074Sminshall 		break;
86130074Sminshall 	     }
86230074Sminshall 	     if ((tcflag = fork()) == 0) {
86330074Sminshall 		(void) close(outpipefd[1]);
86430074Sminshall 		(void) close(0);
86530074Sminshall 		if (dup(outpipefd[0]) < 0) {
86630074Sminshall 		   exit(1);
86730074Sminshall 		}
86830074Sminshall 		(void) close(outpipefd[0]);
86930074Sminshall 		(void) close(inpipefd[0]);
87030074Sminshall 		(void) close(1);
87130074Sminshall 		if (dup(inpipefd[1]) < 0) {
87230074Sminshall 		   exit(1);
87330074Sminshall 		}
87430074Sminshall 		(void) close(inpipefd[1]);
87530074Sminshall 		if (execl("/bin/csh", "csh", "-c", transcom, (char *) 0)) {
87630074Sminshall 		    exit(1);
87730074Sminshall 		}
87830074Sminshall 	     }
87930074Sminshall 	     (void) close(inpipefd[1]);
88030074Sminshall 	     (void) close(outpipefd[0]);
88130074Sminshall 	     savefd[0] = tin;
88230074Sminshall 	     savefd[1] = tout;
88330074Sminshall 	     setcommandmode();
88430074Sminshall 	     tin = inpipefd[0];
88530074Sminshall 	     tout = outpipefd[1];
88630074Sminshall 	     (void) signal(SIGCHLD, aborttc);
88730074Sminshall 	     setconnmode();
88830074Sminshall 	     tcflag = 1;
88930074Sminshall 	     break;
89030074Sminshall        }
89130074Sminshall        if (tcflag < 1) {
89230074Sminshall 	  tcflag = 0;
89330074Sminshall        }
89430074Sminshall     }
89530074Sminshall #endif	/* defined(unix) */
89630074Sminshall     (void) DataToTerminal(buffer, count);
89731863Sminshall     if (control && (kind == 0)) {		/* Send in AID byte */
89831863Sminshall 	SendToIBM();
89931863Sminshall     } else {
90031863Sminshall 	TransInput(1, kind);			/* Go get some data */
90131863Sminshall     }
90230074Sminshall }
90330074Sminshall 
90430074Sminshall 
90530074Sminshall #if	defined(unix)
90630074Sminshall static void
90730074Sminshall aborttc()
90830074Sminshall {
90930074Sminshall 	int savemode;
91030074Sminshall 
91130074Sminshall 	setcommandmode();
91230074Sminshall 	(void) close(tin);
91330074Sminshall 	(void) close(tout);
91430074Sminshall 	tin = savefd[0];
91530074Sminshall 	tout = savefd[1];
91630074Sminshall 	setconnmode();
91730074Sminshall 	tcflag = 0;
91830074Sminshall }
91930074Sminshall #endif	/* defined(unix) */
920