130043Sminshall /*
233810Sbostic  * Copyright (c) 1988 Regents of the University of California.
333810Sbostic  * All rights reserved.
430043Sminshall  *
533810Sbostic  * Redistribution and use in source and binary forms are permitted
633810Sbostic  * provided that this notice is preserved and that due credit is given
733810Sbostic  * to the University of California at Berkeley. The name of the University
833810Sbostic  * may not be used to endorse or promote products derived from this
933810Sbostic  * software without specific prior written permission. This software
1033810Sbostic  * is provided ``as is'' without express or implied warranty.
1130043Sminshall  */
1230043Sminshall 
1330043Sminshall #ifndef lint
14*34315Sminshall static char sccsid[] = "@(#)termout.c	3.6 (Berkeley) 05/16/88";
1533810Sbostic #endif /* not lint */
1630043Sminshall 
1730043Sminshall #if defined(unix)
1830043Sminshall #include <signal.h>
1930043Sminshall #include <sgtty.h>
2030043Sminshall #endif
2130043Sminshall #include <stdio.h>
2230043Sminshall #include <curses.h>
2334307Sminshall #if	defined(ultrix)
2434307Sminshall /* Some version of this OS has a bad definition for nonl() */
2534307Sminshall #undef	nl
2634307Sminshall #undef	nonl
2730043Sminshall 
2834307Sminshall #define nl()	 (_tty.sg_flags |= CRMOD,_pfast = _rawmode,stty(_tty_ch, &_tty))
2934307Sminshall #define nonl()	 (_tty.sg_flags &= ~CRMOD, _pfast = TRUE, stty(_tty_ch, &_tty))
3034307Sminshall #endif	/* defined(ultrix) */
3134307Sminshall 
3231179Sminshall #include "../general/general.h"
3331071Sminshall 
3430043Sminshall #include "terminal.h"
3530043Sminshall 
3630043Sminshall #include "../telnet.ext"
3730043Sminshall 
3831873Sminshall #include "../api/disp_asc.h"
3931224Sminshall 
4030043Sminshall #include "../ctlr/hostctlr.h"
4130043Sminshall #include "../ctlr/inbound.ext"
4231127Sminshall #include "../ctlr/oia.h"
4330043Sminshall #include "../ctlr/options.ext"
4430043Sminshall #include "../ctlr/outbound.ext"
4530043Sminshall #include "../ctlr/screen.h"
4630043Sminshall 
4731179Sminshall #include "../ascii/map3270.ext"
4830043Sminshall 
4931179Sminshall #include "../general/globals.h"
5030043Sminshall 
5130043Sminshall extern void EmptyTerminal();
5230043Sminshall 
5330043Sminshall #define CorrectTerminalCursor() ((TransparentClock == OutputClock)? \
5431839Sminshall 		CursorAddress:UnLocked? CursorAddress: HighestScreen())
5530043Sminshall 
5630043Sminshall 
5730328Sminshall static int terminalCursorAddress;	/* where the cursor is on term */
5830328Sminshall static int screenInitd; 		/* the screen has been initialized */
5930328Sminshall static int screenStopped;		/* the screen has been stopped */
6030043Sminshall static int max_changes_before_poll;	/* how many characters before looking */
6130043Sminshall 					/* at terminal and net again */
6230043Sminshall 
6330328Sminshall static int needToRing;			/* need to ring terinal bell */
6430043Sminshall static char *bellSequence = "\07";	/* bell sequence (may be replaced by
6530043Sminshall 					 * VB during initialization)
6630043Sminshall 					 */
6730328Sminshall static WINDOW *bellwin = 0;		/* The window the bell message is in */
6830043Sminshall int	bellwinup = 0;			/* Are we up with it or not */
6930043Sminshall 
7030043Sminshall #if	defined(unix)
71*34315Sminshall static char *myKS, *myKE;
7230043Sminshall #endif	/* defined(unix) */
7330043Sminshall 
7430074Sminshall 
7530074Sminshall static int inHighlightMode = 0;
7631071Sminshall ScreenImage Terminal[MAXSCREENSIZE];
7730074Sminshall 
7830074Sminshall /* Variables for transparent mode */
7930043Sminshall #if	defined(unix)
8030043Sminshall static int tcflag = -1;			/* transparent mode command flag */
8130043Sminshall static int savefd[2];			/* for storing fds during transcom */
8230074Sminshall extern int	tin, tout;		/* file descriptors */
8330043Sminshall #endif	/* defined(unix) */
8430043Sminshall 
8530730Sminshall 
8630730Sminshall /*
8730730Sminshall  * init_screen()
8830730Sminshall  *
8930730Sminshall  * Initialize variables used by screen.
9030730Sminshall  */
9130730Sminshall 
9230730Sminshall void
9330730Sminshall init_screen()
9430730Sminshall {
9530730Sminshall     bellwinup = 0;
9630730Sminshall     inHighlightMode = 0;
9731071Sminshall     ClearArray(Terminal);
9830730Sminshall }
9930730Sminshall 
10030730Sminshall 
10130043Sminshall /* OurExitString - designed to keep us from going through infinite recursion */
10230043Sminshall 
10330043Sminshall static void
10430043Sminshall OurExitString(file, string, value)
10530043Sminshall FILE	*file;
10630043Sminshall char	*string;
10730043Sminshall int	value;
10830043Sminshall {
10930043Sminshall     static int recursion = 0;
11030043Sminshall 
11130043Sminshall     if (!recursion) {
11230043Sminshall 	recursion = 1;
11330043Sminshall 	ExitString(file, string, value);
11430043Sminshall     }
11530043Sminshall }
11630043Sminshall 
11730043Sminshall 
11830043Sminshall /* DoARefresh */
11930043Sminshall 
12030043Sminshall static void
12130043Sminshall DoARefresh()
12230043Sminshall {
12330043Sminshall     if (ERR == refresh()) {
12430043Sminshall 	OurExitString(stderr, "ERR from refresh\n", 1);
12530043Sminshall     }
12630043Sminshall }
12730043Sminshall 
12830043Sminshall static void
12930043Sminshall GoAway(from, where)
13030043Sminshall char *from;		/* routine that gave error */
13130043Sminshall int	where;		/* cursor address */
13230043Sminshall {
13330043Sminshall 	char foo[100];
13430043Sminshall 
13530043Sminshall 	sprintf(foo, "ERR from %s at %d (%d, %d)\n",
13630043Sminshall 		from, where, ScreenLine(where), ScreenLineOffset(where));
13730043Sminshall 	OurExitString(stderr, foo, 1);
13830043Sminshall 	/* NOTREACHED */
13930043Sminshall }
14030043Sminshall 
14130043Sminshall /* What is the screen address of the attribute byte for the terminal */
14230043Sminshall 
14330043Sminshall static int
14430043Sminshall WhereTermAttrByte(p)
14530043Sminshall register int	p;
14630043Sminshall {
14730043Sminshall     register int i;
14830043Sminshall 
14930043Sminshall     i = p;
15030043Sminshall 
15130043Sminshall     do {
15230043Sminshall 	if (TermIsStartField(i)) {
15330043Sminshall 	    return(i);
15430043Sminshall 	}
15530043Sminshall 	i = ScreenDec(i);
15630043Sminshall     } while (i != p);
15730043Sminshall 
15830043Sminshall     return(LowestScreen());	/* unformatted screen... */
15930043Sminshall }
16030043Sminshall 
16130043Sminshall /*
16230043Sminshall  *	There are two algorithms for updating the screen.
16330043Sminshall  *  The first, SlowScreen() optimizes the line between the
16430043Sminshall  *  computer and the screen (say a 9600 baud line).  To do
16530043Sminshall  *  this, we break out of the loop every so often to look
16630043Sminshall  *  at any pending input from the network (so that successive
16730043Sminshall  *  screens will only partially print until the final screen,
16830043Sminshall  *  the one the user possibly wants to see, is displayed
16930043Sminshall  *  in its entirety).
17030043Sminshall  *
17130043Sminshall  *	The second algorithm tries to optimize CPU time (by
17230043Sminshall  *  being simpler) at the cost of the bandwidth to the
17330043Sminshall  *  screen.
17430043Sminshall  *
17530043Sminshall  *	Of course, curses(3X) gets in here also.
17630043Sminshall  */
17730043Sminshall 
17830043Sminshall 
17930043Sminshall #if	defined(NOT43)
18030043Sminshall static int
18130043Sminshall #else	/* defined(NOT43) */
18230043Sminshall static void
18330043Sminshall #endif	/* defined(NOT43) */
18430043Sminshall SlowScreen()
18530043Sminshall {
18630043Sminshall     register int pointer;
18730043Sminshall     register int c;
18830043Sminshall     register int fieldattr;
18930043Sminshall     register int columnsleft;
19030043Sminshall 
19130043Sminshall #   define  SetHighlightMode(p) { \
19230043Sminshall 		if (!IsStartField(p) && IsHighlightedAttr(fieldattr)) { \
19330043Sminshall 		    if (!inHighlightMode) { \
19430043Sminshall 			inHighlightMode = 1; \
19530043Sminshall 			standout(); \
19630043Sminshall 		    } \
19730043Sminshall 		} else { \
19830043Sminshall 		    if (inHighlightMode) { \
19930043Sminshall 			inHighlightMode = 0; \
20030043Sminshall 			standend(); \
20130043Sminshall 		    } \
20230043Sminshall 		} \
20330043Sminshall 	    }
20430043Sminshall 
20530043Sminshall #   define  DoCharacterAt(c,p) { \
20630043Sminshall 		SetTerminal(p, c); \
20730043Sminshall 		if (p != HighestScreen()) { \
20830043Sminshall 		    c = TerminalCharacterAttr(disp_asc[c&0xff], p, \
20930043Sminshall 								fieldattr); \
21030043Sminshall 		    if (terminalCursorAddress != p) { \
21130043Sminshall 			if (ERR == mvaddch(ScreenLine(p), \
21230043Sminshall 						ScreenLineOffset(p), c)) {\
21330043Sminshall 			    GoAway("mvaddch", p); \
21430043Sminshall 			} \
21530043Sminshall 		    } else { \
21630043Sminshall 			if (ERR == addch(c)) {\
21730043Sminshall 			    GoAway("addch", p); \
21830043Sminshall 			} \
21930043Sminshall 		    } \
22030043Sminshall 		    terminalCursorAddress = ScreenInc(p); \
22130043Sminshall 		} \
22230043Sminshall 	    }
22330043Sminshall 
22430043Sminshall 
22530043Sminshall     /* run through screen, printing out non-null lines */
22630043Sminshall 
22730043Sminshall     /* There are two separate reasons for wanting to terminate this
22830043Sminshall      * loop early.  One is to respond to new input (either from
22930043Sminshall      * the terminal or from the network [host]).  For this reason,
23030043Sminshall      * we expect to see 'HaveInput' come true when new input comes in.
23130043Sminshall      *
23230043Sminshall      * The second reason is a bit more difficult (for me) to understand.
23330043Sminshall      * Basically, we don't want to get too far ahead of the characters that
23430043Sminshall      * appear on the screen.  Ideally, we would type out a few characters,
23530043Sminshall      * wait until they appeared on the screen, then type out a few more.
23630043Sminshall      * The reason for this is that the user, on seeing some characters
23730043Sminshall      * appear on the screen may then start to type something.  We would
23830043Sminshall      * like to look at what the user types at about the same 'time'
23930043Sminshall      * (measured by characters being sent to the terminal) that the
24030043Sminshall      * user types them.  For this reason, what we would like to do
24130043Sminshall      * is update a bit, then call curses to do a refresh, flush the
24230043Sminshall      * output to the terminal, then wait until the terminal data
24330043Sminshall      * has been sent.
24430043Sminshall      *
24530043Sminshall      * Note that curses is useful for, among other things, deciding whether
24630043Sminshall      * or not to send :ce: (clear to end of line), so we should call curses
24730043Sminshall      * at end of lines (beginning of next lines).
24830043Sminshall      *
24930043Sminshall      * The problems here are the following:  If we do lots of write(2)s,
25030043Sminshall      * we will be doing lots of context switches, thus lots of overhead
25130043Sminshall      * (which we have already).  Second, if we do a select to wait for
25230043Sminshall      * the output to drain, we have to contend with the fact that NOW
25330043Sminshall      * we are scheduled to run, but who knows what the scheduler will
25430043Sminshall      * decide when the output has caught up.
25530043Sminshall      */
25630043Sminshall 
25733923Sminshall     if (Highest >= HighestScreen()) {	/* Could be > if screen shrunk... */
25830043Sminshall 	Highest = ScreenDec(Highest);	/* else, while loop will never end */
25930043Sminshall     }
26030043Sminshall     if (Lowest < LowestScreen()) {
26130043Sminshall 	Lowest = LowestScreen();	/* could be -1 in some cases with
26230043Sminshall 					 * unformatted screens.
26330043Sminshall 					 */
26430043Sminshall     }
26530043Sminshall     if (Highest >= (pointer = Lowest)) {
26630043Sminshall 		/* if there is anything to do, do it.  We won't terminate
26730043Sminshall 		 * the loop until we've gone at least to Highest.
26830043Sminshall 		 */
26930043Sminshall 	while ((pointer <= Highest) && !HaveInput) {
27030043Sminshall 
27130043Sminshall 		/* point at the next place of disagreement */
27230043Sminshall 	    pointer += (bunequal(Host+pointer, Terminal+pointer,
27330043Sminshall 			(Highest-pointer+1)*sizeof Host[0])/sizeof Host[0]);
27430043Sminshall 
27530043Sminshall 		/* how many characters to change until the end of the
27630043Sminshall 		 * current line
27730043Sminshall 		 */
27830043Sminshall 	    columnsleft = NumberColumns - ScreenLineOffset(pointer);
27930043Sminshall 		/*
28030043Sminshall 		 * Make sure we are where we think we are.
28130043Sminshall 		 */
28230043Sminshall 	    move(ScreenLine(pointer), ScreenLineOffset(pointer));
28330043Sminshall 
28430043Sminshall 		/* what is the field attribute of the current position */
28530043Sminshall 	    fieldattr = FieldAttributes(WhereAttrByte(pointer));
28630043Sminshall 
28730043Sminshall 	    if ((IsStartField(pointer) != TermIsStartField(pointer)) ||
28830043Sminshall 		    (IsStartField(pointer) &&
28930043Sminshall 			fieldattr != TermAttributes(pointer))) {
29030043Sminshall 
29130043Sminshall 		int oldterm;
29230043Sminshall 
29330043Sminshall 		oldterm = TermAttributes(pointer);
29430043Sminshall 		if (IsStartField(pointer)) {
29530043Sminshall 		    TermNewField(pointer, fieldattr);
29630043Sminshall 		    SetTerminal(pointer, 0);
29730043Sminshall 		} else {
29830043Sminshall 		    TermDeleteField(pointer);
29930043Sminshall 		}
30030043Sminshall 		    /* We always do the first character in a divergent
30130043Sminshall 		     * field, since otherwise the start of a field in
30230043Sminshall 		     * the Host structure may leave a highlighted blank
30330043Sminshall 		     * on the screen, and the start of a field in the
30430043Sminshall 		     * Terminal structure may leave a non-highlighted
30530043Sminshall 		     * something in the middle of a highlighted field
30630043Sminshall 		     * on the screen.
30730043Sminshall 		     */
30830043Sminshall 		SetHighlightMode(pointer);
30930043Sminshall 		c = GetHost(pointer);
31030043Sminshall 		DoCharacterAt(c,pointer);		/* MACRO */
31130043Sminshall 
31230043Sminshall 		if (NotVisuallyCompatibleAttributes
31330043Sminshall 				(pointer, fieldattr, oldterm)) {
31430043Sminshall 		    int j;
31530043Sminshall 
31630043Sminshall 		    j = pointer;
31730043Sminshall 
31830043Sminshall 		    pointer = ScreenInc(pointer);
31930043Sminshall 		    if (!(--columnsleft)) {
32030043Sminshall 			DoARefresh();
32130043Sminshall 			EmptyTerminal();
32230043Sminshall 			move(ScreenLine(pointer), 0);
32330043Sminshall 			columnsleft = NumberColumns;
32430043Sminshall 		    }
32530043Sminshall 		    SetHighlightMode(pointer);	/* Turn on highlighting */
32631452Sminshall 		    while ((!IsStartField(pointer)) &&
32731452Sminshall 				(!TermIsStartField(pointer))) {
32830043Sminshall 			c = GetHost(pointer);
32930043Sminshall 			DoCharacterAt(c,pointer);	/* MACRO */
33030043Sminshall 			pointer = ScreenInc(pointer);
33130043Sminshall 			if (!(--columnsleft)) {
33230043Sminshall 			    DoARefresh();
33330043Sminshall 			    EmptyTerminal();
33430043Sminshall 			    move(ScreenLine(pointer), 0);
33530043Sminshall 			    columnsleft = NumberColumns;
33630043Sminshall 				/* We don't look at HaveInput here, since
33730043Sminshall 				 * if we leave this loop before the end of
33830043Sminshall 				 * the 3270 field, we could have pointer
33930043Sminshall 				 * higher than Highest.  This would cause
34030043Sminshall 				 * us to end the highest "while" loop,
34130043Sminshall 				 * but we may, in fact, need to go around the
34230043Sminshall 				 * screen once again.
34330043Sminshall 				 */
34430043Sminshall 			}
34530043Sminshall 			/*		The loop needs to be protected
34630043Sminshall 			 *	from the situation where there had been only
34730043Sminshall 			 *	one field on the Terminal, and none on the Host.
34830043Sminshall 			 *	In this case, we have just deleted our last
34930043Sminshall 			 *	field.	Hence, the break.
35030043Sminshall 			 */
35130043Sminshall 			if (j == pointer) {
35230043Sminshall 			    break;
35330043Sminshall 			}
35430043Sminshall 		    }
35530043Sminshall 		    if (IsStartField(pointer) && !TermIsStartField(pointer)) {
35630043Sminshall 			    /* Remember what the terminal looked like */
35730043Sminshall 			TermNewField(pointer, oldterm);
35830043Sminshall 			    /*
35930043Sminshall 			     * The danger here is that the current position may
36030043Sminshall 			     * be the start of a Host field.  If so, and the
36130043Sminshall 			     * field is highlighted, and our terminal was
36230043Sminshall 			     * highlighted, then we will leave a highlighted
36330043Sminshall 			     * blank at this position.
36430043Sminshall 			     */
36530043Sminshall 			SetHighlightMode(pointer);
36630043Sminshall 			c = GetHost(pointer);
36730043Sminshall 			DoCharacterAt(c,pointer);
36830043Sminshall 		    }
36930043Sminshall 			/* We could be in the situation of needing to exit.
37030043Sminshall 			 * This could happen if the current field wrapped around
37130043Sminshall 			 * the end of the screen.
37230043Sminshall 			 */
37330043Sminshall 		    if (j > pointer) {
37431452Sminshall 			/*
37531452Sminshall 			 * pointer is guaranteed to be higher than Highest...
37631452Sminshall 			 */
37731452Sminshall 			pointer = Highest+1;	/* We did the highest thing */
37830043Sminshall 			break;
37930043Sminshall 		    }
38030043Sminshall 		} else {
38130043Sminshall 		    c = GetHost(pointer);
38230043Sminshall 			/* We always do the first character in a divergent
38330043Sminshall 			 * field, since otherwise the start of a field in
38430043Sminshall 			 * the Host structure may leave a highlighted blank
38530043Sminshall 			 * on the screen, and the start of a field in the
38630043Sminshall 			 * Terminal structure may leave a non-highlighted
38730043Sminshall 			 * something in the middle of a highlighted field
38830043Sminshall 			 * on the screen.
38930043Sminshall 			 */
39030043Sminshall 		    SetHighlightMode(pointer);
39130043Sminshall 		    DoCharacterAt(c,pointer);
39230043Sminshall 		}
39330043Sminshall 	    } else {
39430043Sminshall 		SetHighlightMode(pointer);
39530054Sminshall 		/*
39630054Sminshall 		 * The following will terminate at least when we get back
39730043Sminshall 		 * to the original 'pointer' location (since we force
39830043Sminshall 		 * things to be equal).
39930043Sminshall 		 */
40030043Sminshall 		while (((c = GetHost(pointer)) != GetTerminal(pointer)) &&
40130043Sminshall 			!IsStartField(pointer) && !TermIsStartField(pointer)) {
40230043Sminshall 		    DoCharacterAt(c, pointer);
40330043Sminshall 		    pointer = ScreenInc(pointer);
40430043Sminshall 		    if (!(--columnsleft)) {
40530043Sminshall 			DoARefresh();
40630043Sminshall 			EmptyTerminal();
40730043Sminshall 			if (HaveInput) {	/* if input came in, take it */
40830043Sminshall 			    break;
40930043Sminshall 			}
41030043Sminshall 			move(ScreenLine(pointer), 0);
41130043Sminshall 			columnsleft = NumberColumns;
41230043Sminshall 		    }
41330043Sminshall 		}
41430043Sminshall 	    }
41530043Sminshall 	}
41630043Sminshall     }
41730043Sminshall     DoARefresh();
41830043Sminshall     Lowest = pointer;
41930043Sminshall     if (Lowest > Highest) {		/* if we finished input... */
42030043Sminshall 	Lowest = HighestScreen()+1;
42130043Sminshall 	Highest = LowestScreen()-1;
42230043Sminshall 	terminalCursorAddress = CorrectTerminalCursor();
42330043Sminshall 	if (ERR == move(ScreenLine(terminalCursorAddress),
42430043Sminshall 			ScreenLineOffset(terminalCursorAddress))) {
42530043Sminshall 	    GoAway("move", terminalCursorAddress);
42630043Sminshall 	}
42730043Sminshall 	DoARefresh();
42830043Sminshall 	if (needToRing) {
42930043Sminshall 	    StringToTerminal(bellSequence);
43030043Sminshall 	    needToRing = 0;
43130043Sminshall 	}
43230043Sminshall     }
43330043Sminshall     EmptyTerminal();			/* move data along */
43430043Sminshall     return;
43530043Sminshall }
43630043Sminshall 
43730043Sminshall #if	defined(NOT43)
43830043Sminshall static int
43930043Sminshall #else	/* defined(NOT43) */
44030043Sminshall static void
44130043Sminshall #endif	/* defined(NOT43) */
44230043Sminshall FastScreen()
44330043Sminshall {
44431101Sminshall #if	defined(MSDOS)
44530043Sminshall #define	SaveCorner	0
44631101Sminshall #else	/* defined(MSDOS) */
44730043Sminshall #define	SaveCorner	1
44831101Sminshall #endif	/* defined(MSDOS) */
44930043Sminshall 
45030043Sminshall #define	DoAttribute(a) 	    if (IsHighlightedAttr(a)) { \
45130043Sminshall 				standout(); \
45230043Sminshall 			    } else { \
45330043Sminshall 				standend(); \
45430043Sminshall 			    } \
45530043Sminshall 			    if (IsNonDisplayAttr(a)) { \
45630043Sminshall 				a = 0; 	/* zero == don't display */ \
45730043Sminshall 			    } \
45830043Sminshall 			    if (!FormattedScreen()) { \
45930043Sminshall 				a = 1;	/* one ==> do display on unformatted */\
46030043Sminshall 			    }
46130043Sminshall     ScreenImage *p, *upper;
46230043Sminshall     int fieldattr;		/* spends most of its time == 0 or 1 */
46330043Sminshall 
46430043Sminshall /* OK.  We want to do this a quickly as possible.  So, we assume we
46530043Sminshall  * only need to go from Lowest to Highest.  However, if we find a
46630043Sminshall  * field in the middle, we do the whole screen.
46730043Sminshall  *
46830043Sminshall  * In particular, we separate out the two cases from the beginning.
46930043Sminshall  */
47030043Sminshall     if ((Highest != HighestScreen()) || (Lowest != LowestScreen())) {
47130043Sminshall 	register int columnsleft;
47230043Sminshall 
47330043Sminshall 	move(ScreenLine(Lowest), ScreenLineOffset(Lowest));
47430043Sminshall 	p = &Host[Lowest];
47531101Sminshall #if	!defined(MSDOS)
47630043Sminshall 	if (Highest == HighestScreen()) {
47730043Sminshall 	    Highest = ScreenDec(Highest);
47830043Sminshall 	}
47931101Sminshall #endif	/* !defined(MSDOS) */
48030043Sminshall 	upper = &Host[Highest];
48130043Sminshall 	fieldattr = FieldAttributes(Lowest);
48230043Sminshall 	DoAttribute(fieldattr);	/* Set standout, non-display status */
48330043Sminshall 	columnsleft = NumberColumns-ScreenLineOffset(p-Host);
48430043Sminshall 
48530043Sminshall 	while (p <= upper) {
48630054Sminshall 	    if (IsStartFieldPointer(p)) {	/* New field? */
48730043Sminshall 		Highest = HighestScreen();
48830043Sminshall 		Lowest = LowestScreen();
48930043Sminshall 		FastScreen();		/* Recurse */
49030043Sminshall 		return;
49130043Sminshall 	    } else if (fieldattr) {	/* Should we display? */
49231148Sminshall 			    /* Display translated data */
49331148Sminshall 		addch(disp_asc[GetTerminalPointer(p)]);
49430043Sminshall 	    } else {
49530043Sminshall 		addch(' ');			/* Display a blank */
49630043Sminshall 	    }
49730043Sminshall 			/* If the physical screen is larger than what we
49830043Sminshall 			 * are using, we need to make sure that each line
49930043Sminshall 			 * starts at the beginning of the line.  Otherwise,
50030043Sminshall 			 * we will just string all the lines together.
50130043Sminshall 			 */
50230043Sminshall 	    p++;
50330043Sminshall 	    if (--columnsleft == 0) {
50430043Sminshall 		int i = p-Host;
50530043Sminshall 
50630043Sminshall 		move(ScreenLine(i), 0);
50730043Sminshall 		columnsleft = NumberColumns;
50830043Sminshall 	    }
50930043Sminshall 	}
51030043Sminshall     } else {		/* Going from Lowest to Highest */
51130043Sminshall 	unsigned char tmpbuf[MAXNUMBERCOLUMNS+1];
51230043Sminshall 	ScreenImage *End = &Host[ScreenSize]-1-SaveCorner;
51330043Sminshall 	register unsigned char *tmp = tmpbuf, *tmpend = tmpbuf+NumberColumns;
51430043Sminshall 
51530043Sminshall 	*tmpend = 0;		/* terminate from the beginning */
51630043Sminshall 	move(0,0);
51730043Sminshall 	p = Host;
51830043Sminshall 	fieldattr = FieldAttributes(LowestScreen());
51930043Sminshall 	DoAttribute(fieldattr);	/* Set standout, non-display status */
52030043Sminshall 
52130043Sminshall 	while (p <= End) {
52230054Sminshall 	    if (IsStartFieldPointer(p)) {	/* New field? */
52330043Sminshall 		if (tmp != tmpbuf) {
52430043Sminshall 		    *tmp++ = 0;			/* close out */
52530043Sminshall 		    addstr(tmpbuf);
52630043Sminshall 		    tmp = tmpbuf;
52730043Sminshall 		    tmpend = tmpbuf + NumberColumns - ScreenLineOffset(p-Host);
52830043Sminshall 		}
52930054Sminshall 		fieldattr = FieldAttributesPointer(p);	/* Get attributes */
53030043Sminshall 		DoAttribute(fieldattr);	/* Set standout, non-display */
53130043Sminshall 		*tmp++ = ' ';
53230043Sminshall 	    } else {
53330043Sminshall 		if (fieldattr) {	/* Should we display? */
53430043Sminshall 				/* Display translated data */
53531148Sminshall 		    *tmp++ = disp_asc[GetTerminalPointer(p)];
53630043Sminshall 		} else {
53730043Sminshall 		    *tmp++ = ' ';
53830043Sminshall 		}
53930043Sminshall 	    }
54030043Sminshall 			/* If the physical screen is larger than what we
54130043Sminshall 			 * are using, we need to make sure that each line
54230043Sminshall 			 * starts at the beginning of the line.  Otherwise,
54330043Sminshall 			 * we will just string all the lines together.
54430043Sminshall 			 */
54530043Sminshall 	    p++;
54630043Sminshall 	    if (tmp == tmpend) {
54730043Sminshall 		int i = p-Host;		/* Be sure the "p++" happened first! */
54830043Sminshall 
54930043Sminshall 		*tmp++ = 0;
55030043Sminshall 		addstr(tmpbuf);
55130043Sminshall 		tmp = tmpbuf;
55230043Sminshall 		move(ScreenLine(i), 0);
55330043Sminshall 		tmpend = tmpbuf + NumberColumns;
55430043Sminshall 	    }
55530043Sminshall 	}
55630043Sminshall 	if (tmp != tmpbuf) {
55730043Sminshall 	    *tmp++ = 0;
55830043Sminshall 	    addstr(tmpbuf);
55930043Sminshall 	    tmp = tmpbuf;
56030043Sminshall 	}
56130043Sminshall     }
56230043Sminshall     Lowest = HighestScreen()+1;
56330043Sminshall     Highest = LowestScreen()-1;
56430043Sminshall     terminalCursorAddress = CorrectTerminalCursor();
56530043Sminshall     if (ERR == move(ScreenLine(terminalCursorAddress),
56630043Sminshall 		    ScreenLineOffset(terminalCursorAddress))) {
56730043Sminshall 	GoAway("move", terminalCursorAddress);
56830043Sminshall     }
56930043Sminshall     DoARefresh();
57030043Sminshall     if (needToRing) {
57130043Sminshall 	StringToTerminal(bellSequence);
57230043Sminshall 	needToRing = 0;
57330043Sminshall     }
57430043Sminshall     EmptyTerminal();			/* move data along */
57530043Sminshall     return;
57630043Sminshall }
57730043Sminshall 
57830043Sminshall 
57930043Sminshall /* TryToSend - send data out to user's terminal */
58030043Sminshall 
58130043Sminshall #if	defined(NOT43)
58230043Sminshall int
58330043Sminshall #else	/* defined(NOT43) */
58430043Sminshall void
58530043Sminshall #endif	/* defined(NOT43) */
58630043Sminshall 	(*TryToSend)() = FastScreen;
58730043Sminshall 
58831127Sminshall void
58931127Sminshall ScreenOIA(oia)
59031127Sminshall OIA *oia;
59131127Sminshall {
59231127Sminshall }
59331127Sminshall 
59431127Sminshall 
59530328Sminshall /* InitTerminal - called to initialize the screen, etc. */
59630043Sminshall 
59730043Sminshall void
59830328Sminshall InitTerminal()
59930043Sminshall {
60030043Sminshall #if defined(unix)
60130043Sminshall     struct sgttyb ourttyb;
60230043Sminshall     static int speeds[] = { 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800,
60330043Sminshall 		2400, 4800, 9600 };
60430043Sminshall #endif
60530043Sminshall 
60630328Sminshall     InitMapping();		/* Go do mapping file (MAP3270) first */
60730043Sminshall     if (!screenInitd) { 	/* not initialized */
60830043Sminshall #if	defined(unix)
60930043Sminshall 	char KSEbuffer[2050];
61030043Sminshall 	char *lotsofspace = KSEbuffer;
61130043Sminshall 	extern int abort();
61230043Sminshall 	extern char *tgetstr();
61330043Sminshall #endif	/* defined(unix) */
61430043Sminshall 
61534294Sminshall 	if (initscr() == ERR) {	/* Initialize curses to get line size */
61634294Sminshall 	    ExitString("InitTerminal:  Error initializing curses", 1);
61734294Sminshall 	    /*NOTREACHED*/
61834294Sminshall 	}
61934294Sminshall 	MaxNumberLines = LINES;
62034294Sminshall 	MaxNumberColumns = COLS;
62131101Sminshall 	ClearArray(Terminal);
62230328Sminshall 	terminalCursorAddress = SetBufferAddress(0,0);
62330043Sminshall #if defined(unix)
62430043Sminshall 	signal(SIGHUP, abort);
62530043Sminshall #endif
62630043Sminshall 
62730043Sminshall 	TryToSend = FastScreen;
62831559Sminshall #if defined(unix)
62930043Sminshall 	ioctl(1, TIOCGETP, (char *) &ourttyb);
63030043Sminshall 	if ((ourttyb.sg_ospeed < 0) || (ourttyb.sg_ospeed > B9600)) {
63130043Sminshall 	    max_changes_before_poll = 1920;
63230043Sminshall 	} else {
63330043Sminshall 	    max_changes_before_poll = speeds[ourttyb.sg_ospeed]/10;
63430043Sminshall 	    if (max_changes_before_poll < 40) {
63530043Sminshall 		max_changes_before_poll = 40;
63630043Sminshall 	    }
63730043Sminshall 	    TryToSend = SlowScreen;
63830043Sminshall 	    HaveInput = 1;		/* get signals going */
63930043Sminshall 	}
64031559Sminshall #endif	/* defined(unix) */
64130043Sminshall 	setcommandmode();
64230043Sminshall 	/*
64330043Sminshall 	 * By now, initscr() (in curses) has been called (from telnet.c),
64430043Sminshall 	 * and the screen has been initialized.
64530043Sminshall 	 */
64630043Sminshall #if defined(unix)
64730043Sminshall 	nonl();
64830043Sminshall 			/* the problem is that curses catches SIGTSTP to
64930043Sminshall 			 * be nice, but it messes us up.
65030043Sminshall 			 */
65130043Sminshall 	signal(SIGTSTP, SIG_DFL);
652*34315Sminshall 	if ((myKS = tgetstr("ks", &lotsofspace)) != 0) {
653*34315Sminshall 	    myKS = strsave(myKS);
654*34315Sminshall 	    StringToTerminal(myKS);
65530043Sminshall 	}
656*34315Sminshall 	if ((myKE = tgetstr("ke", &lotsofspace)) != 0) {
657*34315Sminshall 	    myKE = strsave(myKE);
65830043Sminshall 	}
65930043Sminshall 	if (tgetstr("md", &lotsofspace) && tgetstr("me", &lotsofspace)) {
66030043Sminshall 	   SO = strsave(tgetstr("md", &lotsofspace));
66130043Sminshall 	   SE = strsave(tgetstr("me", &lotsofspace));
66230043Sminshall 	}
66330043Sminshall #endif
66430043Sminshall 	DoARefresh();
66530043Sminshall 	setconnmode();
66630043Sminshall 	if (VB && *VB) {
66730043Sminshall 	    bellSequence = VB;		/* use visual bell */
66830043Sminshall 	}
66930043Sminshall 	screenInitd = 1;
67030043Sminshall 	screenStopped = 0;		/* Not stopped */
67130043Sminshall     }
67230043Sminshall }
67330043Sminshall 
67430043Sminshall 
67530043Sminshall /* StopScreen - called when we are going away... */
67630043Sminshall 
67730043Sminshall void
67830043Sminshall StopScreen(doNewLine)
67930043Sminshall int doNewLine;
68030043Sminshall {
68130043Sminshall     if (screenInitd && !screenStopped) {
68230043Sminshall 	move(NumberLines-1, 1);
68330043Sminshall 	standend();
68430043Sminshall 	inHighlightMode = 0;
68530043Sminshall 	DoARefresh();
68630043Sminshall 	setcommandmode();
68730043Sminshall 	endwin();
68830043Sminshall 	setconnmode();
68930043Sminshall #if	defined(unix)
690*34315Sminshall 	if (myKE) {
691*34315Sminshall 	    StringToTerminal(myKE);
69230043Sminshall 	}
69330043Sminshall #endif	/* defined(unix) */
69430043Sminshall 	if (doNewLine) {
69530043Sminshall 	    StringToTerminal("\r\n");
69630043Sminshall 	}
69730043Sminshall 	EmptyTerminal();
69830043Sminshall 	screenStopped = 1;		/* This is stopped */
69930043Sminshall     }
70030043Sminshall }
70130043Sminshall 
70230043Sminshall 
70330043Sminshall /* RefreshScreen - called to cause the screen to be refreshed */
70430043Sminshall 
70530043Sminshall void
70630043Sminshall RefreshScreen()
70730043Sminshall {
70830043Sminshall     clearok(curscr, TRUE);
70930043Sminshall     (*TryToSend)();
71030043Sminshall }
71130043Sminshall 
71230043Sminshall 
71330043Sminshall /* ConnectScreen - called to reconnect to the screen */
71430043Sminshall 
71530043Sminshall void
71630043Sminshall ConnectScreen()
71730043Sminshall {
71830043Sminshall     if (screenInitd) {
71930043Sminshall #if	defined(unix)
720*34315Sminshall 	if (myKS) {
721*34315Sminshall 	    StringToTerminal(myKS);
72230043Sminshall 	}
72330043Sminshall #endif	/* defined(unix) */
72430043Sminshall 	RefreshScreen();
72530043Sminshall 	(*TryToSend)();
72630043Sminshall 	screenStopped = 0;
72730043Sminshall     }
72830043Sminshall }
72930043Sminshall 
73030043Sminshall /* LocalClearScreen() - clear the whole ball of wax, cheaply */
73130043Sminshall 
73230043Sminshall void
73330043Sminshall LocalClearScreen()
73430043Sminshall {
73530043Sminshall     outputPurge();		/* flush all data to terminal */
73630043Sminshall     clear();			/* clear in curses */
73731101Sminshall     ClearArray(Terminal);
73830043Sminshall     Clear3270();
73930043Sminshall     Lowest = HighestScreen()+1; /* everything in sync... */
74030043Sminshall     Highest = LowestScreen()+1;
74130043Sminshall }
74230043Sminshall 
74330043Sminshall 
74430043Sminshall void
74530043Sminshall BellOff()
74630043Sminshall {
74730043Sminshall     if (bellwinup) {
74830043Sminshall 	delwin(bellwin);
74930043Sminshall 	bellwin = 0;
75030043Sminshall 	bellwinup = 0;
75130043Sminshall 	touchwin(stdscr);
75230043Sminshall 	DoARefresh();
75330043Sminshall     }
75430043Sminshall }
75530043Sminshall 
75630043Sminshall 
75730043Sminshall void
75830043Sminshall RingBell(s)
75930043Sminshall char *s;
76030043Sminshall {
76130043Sminshall     needToRing = 1;
76230043Sminshall     if (s) {
76330043Sminshall 	int len = strlen(s);
76430043Sminshall 
76530043Sminshall 	if (len > COLS-2) {
76630043Sminshall 	    len = COLS-2;
76730043Sminshall 	}
76830043Sminshall 	if ((bellwin = newwin(3, len+2, LINES/2, 0)) == NULL) {
76930043Sminshall 	    OurExitString(stderr, "Error from newwin in RingBell", 1);
77030043Sminshall 	}
77130043Sminshall 	werase(bellwin);
77230043Sminshall 	wstandout(bellwin);
77330043Sminshall 	box(bellwin, '|', '-');
77430043Sminshall 	if (wmove(bellwin, 1, 1) == ERR) {
77530043Sminshall 	    OurExitString(stderr, "Error from wmove in RingBell", 1);
77630043Sminshall 	}
77730043Sminshall 	while (len--) {
77830043Sminshall 	    if (waddch(bellwin, *s++) == ERR) {
77930043Sminshall 		OurExitString(stderr, "Error from waddch in RingBell", 1);
78030043Sminshall 	    }
78130043Sminshall 	}
78230043Sminshall 	wstandend(bellwin);
78330043Sminshall 	if (wrefresh(bellwin) == ERR) {
78430043Sminshall 	    OurExitString(stderr, "Error from wrefresh in RingBell", 1);
78530043Sminshall 	}
78630043Sminshall 	bellwinup = 1;
78730043Sminshall     }
78830043Sminshall }
78930043Sminshall 
79030043Sminshall 
79130043Sminshall /* returns a 1 if no more output available (so, go ahead and block),
79230043Sminshall     or a 0 if there is more output available (so, just poll the other
79330043Sminshall     sources/destinations, don't block).
79430043Sminshall  */
79530043Sminshall 
79630043Sminshall int
79730043Sminshall DoTerminalOutput()
79830043Sminshall {
79930043Sminshall 	/* called just before a select to conserve IO to terminal */
80031462Sminshall     if (!(screenInitd||screenStopped)) {
80130369Sminshall 	return 1;		/* No output if not initialized */
80230369Sminshall     }
80330369Sminshall     if ((Lowest <= Highest) || needToRing ||
80430369Sminshall 			(terminalCursorAddress != CorrectTerminalCursor())) {
80530043Sminshall 	(*TryToSend)();
80630043Sminshall     }
80730043Sminshall     if (Lowest > Highest) {
80830369Sminshall 	return 1;		/* no more output now */
80930043Sminshall     } else {
81030369Sminshall 	return 0;		/* more output for future */
81130043Sminshall     }
81230043Sminshall }
81330074Sminshall 
81430074Sminshall /*
81530074Sminshall  * The following are defined to handle transparent data.
81630074Sminshall  */
81730074Sminshall 
81830074Sminshall void
81930074Sminshall TransStop()
82030074Sminshall {
82130074Sminshall #if	defined(unix)
82230074Sminshall     if (tcflag == 0) {
82330074Sminshall        tcflag = -1;
82430074Sminshall        (void) signal(SIGCHLD, SIG_DFL);
82530074Sminshall     } else if (tcflag > 0) {
82630074Sminshall        setcommandmode();
82730074Sminshall        (void) close(tin);
82830074Sminshall        (void) close(tout);
82930074Sminshall        tin = savefd[0];
83030074Sminshall        tout = savefd[1];
83130074Sminshall        setconnmode();
83230074Sminshall        tcflag = -1;
83330074Sminshall        (void) signal(SIGCHLD, SIG_DFL);
83430074Sminshall     }
83530074Sminshall #endif	/* defined(unix) */
83630074Sminshall     RefreshScreen();
83730074Sminshall }
83830074Sminshall 
83930074Sminshall void
84031863Sminshall TransOut(buffer, count, kind, control)
84130074Sminshall unsigned char	*buffer;
84230074Sminshall int		count;
84331863Sminshall int		kind;		/* 0 or 5 */
84431863Sminshall int		control;	/* To see if we are done */
84530074Sminshall {
84630074Sminshall #if	defined(unix)
84730074Sminshall     extern char *transcom;
84830074Sminshall     int inpipefd[2], outpipefd[2], savemode;
84930074Sminshall     void aborttc();
85030074Sminshall #endif	/* defined(unix) */
85130074Sminshall 
85230074Sminshall     while (DoTerminalOutput() == 0) {
85330074Sminshall #if defined(unix)
85430074Sminshall 	HaveInput = 0;
85530074Sminshall #endif /* defined(unix) */
85630074Sminshall     }
85730074Sminshall #if	defined(unix)
85830074Sminshall     if (transcom && tcflag == -1) {
85930074Sminshall        while (1) {			  /* go thru once */
86030074Sminshall 	     if (pipe(outpipefd) < 0) {
86130074Sminshall 		break;
86230074Sminshall 	     }
86330074Sminshall 	     if (pipe(inpipefd) < 0) {
86430074Sminshall 		break;
86530074Sminshall 	     }
86630074Sminshall 	     if ((tcflag = fork()) == 0) {
86730074Sminshall 		(void) close(outpipefd[1]);
86830074Sminshall 		(void) close(0);
86930074Sminshall 		if (dup(outpipefd[0]) < 0) {
87030074Sminshall 		   exit(1);
87130074Sminshall 		}
87230074Sminshall 		(void) close(outpipefd[0]);
87330074Sminshall 		(void) close(inpipefd[0]);
87430074Sminshall 		(void) close(1);
87530074Sminshall 		if (dup(inpipefd[1]) < 0) {
87630074Sminshall 		   exit(1);
87730074Sminshall 		}
87830074Sminshall 		(void) close(inpipefd[1]);
87930074Sminshall 		if (execl("/bin/csh", "csh", "-c", transcom, (char *) 0)) {
88030074Sminshall 		    exit(1);
88130074Sminshall 		}
88230074Sminshall 	     }
88330074Sminshall 	     (void) close(inpipefd[1]);
88430074Sminshall 	     (void) close(outpipefd[0]);
88530074Sminshall 	     savefd[0] = tin;
88630074Sminshall 	     savefd[1] = tout;
88730074Sminshall 	     setcommandmode();
88830074Sminshall 	     tin = inpipefd[0];
88930074Sminshall 	     tout = outpipefd[1];
89030074Sminshall 	     (void) signal(SIGCHLD, aborttc);
89130074Sminshall 	     setconnmode();
89230074Sminshall 	     tcflag = 1;
89330074Sminshall 	     break;
89430074Sminshall        }
89530074Sminshall        if (tcflag < 1) {
89630074Sminshall 	  tcflag = 0;
89730074Sminshall        }
89830074Sminshall     }
89930074Sminshall #endif	/* defined(unix) */
90030074Sminshall     (void) DataToTerminal(buffer, count);
90131863Sminshall     if (control && (kind == 0)) {		/* Send in AID byte */
90231863Sminshall 	SendToIBM();
90331863Sminshall     } else {
90431863Sminshall 	TransInput(1, kind);			/* Go get some data */
90531863Sminshall     }
90630074Sminshall }
90730074Sminshall 
90830074Sminshall 
90930074Sminshall #if	defined(unix)
91030074Sminshall static void
91130074Sminshall aborttc()
91230074Sminshall {
91330074Sminshall 	int savemode;
91430074Sminshall 
91530074Sminshall 	setcommandmode();
91630074Sminshall 	(void) close(tin);
91730074Sminshall 	(void) close(tout);
91830074Sminshall 	tin = savefd[0];
91930074Sminshall 	tout = savefd[1];
92030074Sminshall 	setconnmode();
92130074Sminshall 	tcflag = 0;
92230074Sminshall }
92330074Sminshall #endif	/* defined(unix) */
924